【发布时间】:2017-04-10 20:15:42
【问题描述】:
我有一个使用 UIPanGestureRecognizer 的可移动图像,我需要使图像越靠近屏幕边缘越透明。 下面是我用来移动和旋转图像的代码,它离中心越远。
将 UIPanGestureRecognizer 添加到图像所在的 UIView:
let moveImage = UIPanGestureRecognizer(target: self, action: #selector(self.detectPan))
moveImage.cancelsTouchesInView = false
MainImageView.addGestureRecognizer(moveImage)
UIPanGestureRecognizer 启动时调用的函数。
func detectPan(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed {
let translation = gesture.translation(in: self.view)
gesture.view!.center = CGPoint(x: gesture.view!.center.x + translation.x, y: gesture.view!.center.y)
gesture.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
let newValue = CGFloat(((gesture.view!.center.x + translation.x) - (self.view.bounds.width * 0.50)) / 500)
MainImageView.transform = MainImageView.transform.rotated(by: -lastValue)
MainImageView.transform = MainImageView.transform.rotated(by: newValue)
lastValue = newValue
}
}
【问题讨论】:
标签: ios swift swift3 uipangesturerecognizer