https://zeddios.tistory.com/343 Pinch 기본적인 코드는 해당 블로그 참고해서 작성
https://github.com/iOS-junsoo/Pinch 깃헙주소
1. 제한 없이 이미지 확대 축소하기
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
myView.isUserInteractionEnabled = true
}
var recognizerScale: CGFloat = 1.0
var maxScale: CGFloat = 2.0
var minScale: CGFloat = 1.0
@IBOutlet weak var myView: UIView!
@IBOutlet var PinchGestureRecognizer: UIPinchGestureRecognizer!
@IBAction func pinchAction(_ sender: Any) {
myView.transform = myView.transform.scaledBy(x: PinchGestureRecognizer.scale, y: PinchGestureRecognizer.scale)
PinchGestureRecognizer.scale = 1.0
}
}
2. 확대 축소 크기 제한두기
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
myView.isUserInteractionEnabled = true
}
var recognizerScale: CGFloat = 1.0
var maxScale: CGFloat = 2.0
var minScale: CGFloat = 1.0
@IBOutlet weak var myView: UIView!
@IBOutlet var PinchGestureRecognizer: UIPinchGestureRecognizer!
@IBAction func pinchAction(_ sender: Any) {
if PinchGestureRecognizer.state == .began || PinchGestureRecognizer.state == .changed{
if recognizerScale < maxScale && PinchGestureRecognizer.scale > 1.0 {
myView.transform = myView.transform.scaledBy(x: PinchGestureRecognizer.scale, y: PinchGestureRecognizer.scale)
recognizerScale *= PinchGestureRecognizer.scale
PinchGestureRecognizer.scale = 1.0
}
else if recognizerScale > minScale && PinchGestureRecognizer.scale < 1.0 {
myView.transform = myView.transform.scaledBy(x: PinchGestureRecognizer.scale, y: PinchGestureRecognizer.scale)
recognizerScale *= PinchGestureRecognizer.scale
PinchGestureRecognizer.scale = 1.0
}
}
}
}
3. 확대 축소 제한두고 너무 축소했을 때 원래대로 복귀
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
myView.isUserInteractionEnabled = true
}
var recognizerScale: CGFloat = 1.0 //이미지 스케일의 크기
var maxScale: CGFloat = 2.0
var minScale: CGFloat = 1.0
@IBOutlet weak var myView: UIView!
@IBOutlet var PinchGestureRecognizer: UIPinchGestureRecognizer!// 두 손가락의 거리
@IBAction func pinchAction(_ sender: Any) {
if PinchGestureRecognizer.state == .began || PinchGestureRecognizer.state == .changed{
if recognizerScale < maxScale && PinchGestureRecognizer.scale > 1.0 {
myView.transform = myView.transform.scaledBy(x: PinchGestureRecognizer.scale, y: PinchGestureRecognizer.scale)
recognizerScale *= PinchGestureRecognizer.scale
PinchGestureRecognizer.scale = 1.0
}
else if recognizerScale > 0.5 && PinchGestureRecognizer.scale < 1.0 {
myView.transform = myView.transform.scaledBy(x: PinchGestureRecognizer.scale, y: PinchGestureRecognizer.scale)
recognizerScale *= PinchGestureRecognizer.scale
PinchGestureRecognizer.scale = 1.0
}
}
else {
if recognizerScale < minScale {
myView.transform = CGAffineTransform.identity
PinchGestureRecognizer.scale = 1.0
}
}
}
}
'iOS - 실무관련 > iOS' 카테고리의 다른 글
Delegate 패턴 (0) | 2022.06.12 |
---|---|
textView행간 조절, 정렬, 폰트, 폰트 사이즈 설정하기 (0) | 2022.06.08 |
원형 View 만들기 (0) | 2022.05.22 |
SceneDelegate에서 특정 ViewController 보여지게 만들기 (0) | 2022.05.08 |
DatePickerView 24h 설정하기 (0) | 2022.04.17 |