要使用UIImageView调整图片大小以便我可以放大和缩小,您可以使用以下步骤:
import UIKit
class ViewController: UIViewController {
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "your_image_name")
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageView.center = view.center
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
}
}
class ViewController: UIViewController {
let imageView = UIImageView()
var lastScale: CGFloat = 1.0
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "your_image_name")
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageView.center = view.center
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
imageView.addGestureRecognizer(pinchGesture)
imageView.isUserInteractionEnabled = true
}
@objc func pinchAction(sender: UIPinchGestureRecognizer) {
switch sender.state {
case .began, .changed:
let scale = sender.scale
imageView.transform = imageView.transform.scaledBy(x: scale, y: scale)
sender.scale = 1.0
default:
break
}
}
}
现在,您可以在模拟器或实际设备上运行您的应用程序,并使用双指手势来放大和缩小图像。
以上就是如何使用UIImageView调整图片大小以便我可以放大和缩小的方法。
领取专属 10元无门槛券
手把手带您无忧上云