gpt4 book ai didi

ios - 使用 CGAffineTransform 和 PanGestureRecognizer 旋转 ImageView

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:46 25 4
gpt4 key购买 nike

我正在尝试使用 CGAffineTransform 旋转我拥有的 ImageView。基本上,我想要发生的是每次 x 坐标增加时,我希望 ImageView 旋转多一点。

我计算了一下,我希望 ImageView 每 1 个 x 坐标旋转 1.6º。这是我目前在 gesture recognizer 函数中的内容:

@objc func personDragRecognizer(recognizer: UIPanGestureRecognizer) {

let rotationAngle: CGFloat = 1.6

let translation = recognizer.translation(in: rView)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y)
view.transform = CGAffineTransform(rotationAngle: rotationAngle)
}
recognizer.setTranslation(CGPoint.zero, in: rView)

if recognizer.view?.center == CGPoint(x: 197.5, y: 232.5) {
PlaygroundPage.current.liveView = eeView
}

问题在于它旋转576º bc,即1.6 乘以 360。而且它不会继续旋转它只会旋转一次。我希望它不断旋转。

如果有人能提供帮助,我们将不胜感激。非常感谢!!

干杯,西奥

最佳答案

我不确定我是否完全理解您要实现的功能。这是负责图像旋转的 UIView 子类的示例。

class CustomView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)

let panGR = UIPanGestureRecognizer(target: self, action: #selector(panGestureDetected(sender:)))
self.gestureRecognizers = [panGR]
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

var gestureBeginning: CGFloat = 0.0

func panGestureDetected(sender: UIPanGestureRecognizer) {
guard sender.numberOfTouches > 0 else { return }

let touchPoint = sender.location(ofTouch: 0, in: self)

switch sender.state {
case .began:
gestureBeginning = touchPoint.x
print("gestureBeginning: \(gestureBeginning)")
case .changed:
let progress = touchPoint.x - gestureBeginning
print("progress: \(progress)")
let rotationAngle: CGFloat = 1.6 * progress
let view = self.subviews[0]
let rads = rotationAngle * .pi / 180
view.transform = CGAffineTransform(rotationAngle: rads)
default:
break
}
}
}

在 Playground 上运行它:

let imageView = UIImageView(image: #imageLiteral(resourceName: "image.png"))
imageView.frame = CGRect(origin: CGPoint(x: 50.0, y: 50.0), size: CGSize(width: 150.0, height: 150.0))

let containerView = CustomView(frame: CGRect(origin: .zero, size: CGSize(width: 250.0, height: 250.0)))
containerView.addSubview(imageView)

PlaygroundPage.current.liveView = containerView
PlaygroundPage.current.needsIndefiniteExecution = true

关于ios - 使用 CGAffineTransform 和 PanGestureRecognizer 旋转 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43072661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com