반응형
iOS 15.5, Xcode 13.31, Swift 5, UIKit 환경에서 진행했습니다.
import UIKit
class UIRotationViewController: UIViewController {
lazy var label: UILabel = {
var label = UILabel()
label.text = "rotate test"
label.frame = CGRect(x: 50, y: 50, width: 200, height: 50)
label.backgroundColor = .red
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(handleRotate(_:)))
view.addGestureRecognizer(rotateGesture)
}
@objc func handleRotate(_ gesture: UIRotationGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.changed {
let transform = CGAffineTransform(rotationAngle: gesture.rotation)
view.transform = transform
}
}
}
Rotate 기능을 사용하고 싶은 View에 addGestureRecognizer 메소드를 통해 Gesture를 등록하여 Rotate 기능을 사용할 수 있다.
반응형
'iOS > 코드조각' 카테고리의 다른 글
[iOS, Swift] Timer 기본 예제 (0) | 2022.08.28 |
---|---|
[iOS, Swift] UIBezierPath 예제 (0) | 2022.08.25 |
[Xcode, Simulator] Xcode simulator gesture 기능(pinch, rotate) (0) | 2022.08.25 |
[iOS, Swift] UIPinchGestureRecognizer 예제 (0) | 2022.08.25 |
[iOS, Swift] UISwipeGestureRecognizer 예제 (0) | 2022.08.25 |