[英]How do I end UILongPressGestureRecognizer with an animation?
I've animated a UIImageView with a UILongPressGestureRecognizer
like so: 我使用UILongPressGestureRecognizer
为UIImageView设置了动画, UILongPressGestureRecognizer
所示:
override func viewDidAppear(animated: Bool) {
var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress"))
imageView.addGestureRecognizer(longPress)
imageView.userInteractionEnabled = true
}
func longPress()
{
let bounds = self.imageView.bounds
UIView.animateWithDuration(0.5, animations: {
self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
})
println("user pressed on image")
}
the animation just causes the UIImageview's height and width to expand on the longPress, however when the press is released, the bounds continue to grow.. How do I return the bounds of the UIImageView to the origin width and height when the press is released? 动画只会导致UIImageview的高度和宽度在longPress上展开,但是当释放按下时,边界会继续增长。当释放按下时,如何将UIImageView的边界返回到原点宽度和高度?
Try this: 尝试这个:
var oldbounds:CGRect!
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))
longPress.minimumPressDuration = 0.01
image.addGestureRecognizer(longPress)
image.userInteractionEnabled = true
}
func longPress(gesture:UILongPressGestureRecognizer)
{
if gesture.state == UIGestureRecognizerState.Began
{
oldbounds = self.image.bounds
}
else if gesture.state == UIGestureRecognizerState.Changed
{
let bounds = self.image.bounds
UIView.animateWithDuration(0.5, animations: {
self.image.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 22, height: bounds.size.height + 22)
})
println("user pressed on image")
}
else
{
let bounds = self.image.bounds
UIView.animateWithDuration(0.5, animations: {
self.image.bounds = self.oldbounds
})
println("user release on image")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.