如何在屏幕上动画时使 UIImageView 可点击
how to make a UIImageView tappable while it is animating across the screen
我有一个 for in 循环,它生成在屏幕上移动的点图像。我不知道在哪里或如何实现一个点击手势识别器,该识别器将应用于所有正在生成的点,并且能够在它们被点击时消失 moving/animating。请帮帮我!谢谢!
@IBAction func animateButton(sender: AnyObject) {
self.view.userInteractionEnabled = true
// Declare delay counter
var delayCounter:Int = 100000
var durationCounter:Double = 0
// loop for 1000 times
for loopNumber in 0...1000 {
// set up some constants for the animations
let dotDuration:Double = 4 - durationCounter
let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let blueDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let yellowDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let greenDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let options = UIViewAnimationOptions.AllowUserInteraction
//set up some constants for the dots
let redSize:CGFloat = 54
let redYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let blueSize:CGFloat = 54
let blueYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let yellowSize:CGFloat = 54
let yellowYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let greenSize:CGFloat = 54
let greenYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
// create the dots and add them to the view
let redDot = UIImageView()
redDot.image = UIImage(named: "Red Dot")
redDot.frame = CGRectMake(0-redSize, redYPosition, redSize, redSize)
self.view.addSubview(redDot)
let blueDot = UIImageView()
blueDot.image = UIImage(named: "Blue Dot")
blueDot.frame = CGRectMake(0-blueSize, blueYPosition, blueSize, blueSize)
self.view.addSubview(blueDot)
let yellowDot = UIImageView()
yellowDot.image = UIImage(named: "Yellow Dot")
yellowDot.frame = CGRectMake(0-yellowSize, yellowYPosition, yellowSize, yellowSize)
self.view.addSubview(yellowDot)
let greenDot = UIImageView()
greenDot.image = UIImage(named: "Green Dot")
greenDot.frame = CGRectMake(0-greenSize, greenYPosition, greenSize, greenSize)
self.view.addSubview(greenDot)
// define the animations
UIView.animateWithDuration(dotDuration , delay: redDelay, options: options, animations: {
redDot.frame = CGRectMake(675, redYPosition, redSize, redSize)
}, completion: { animationFinished in redDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: blueDelay, options: options, animations: {
blueDot.frame = CGRectMake(675, blueYPosition, blueSize, blueSize)
}, completion: { animationFinished in blueDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: yellowDelay, options: options, animations: {
yellowDot.frame = CGRectMake(675, yellowYPosition, yellowSize, yellowSize)
}, completion: { animationFinished in yellowDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: greenDelay, options: options, animations: {
greenDot.frame = CGRectMake(675, greenYPosition, greenSize, greenSize)
}, completion: { animationFinished in greenDot.removeFromSuperview() })
// FAILED ATTEMPT
var tapDot:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dotTapped")
// FAILED ATTEMPT
redDot.addGestureRecognizer(tapDot)
// FAILED ATTEMPT
func dotTapped (recognizer: UITapGestureRecognizer) {
redDot.alpha = 0
}
durationCounter+=0.05
delayCounter+=50000
}
}
- 您的
dotTapped
方法应该在您的 animateButton
方法之外。
- 确保
userInteractionEnabled
对于 UIImageView
是正确的。
我有一个 for in 循环,它生成在屏幕上移动的点图像。我不知道在哪里或如何实现一个点击手势识别器,该识别器将应用于所有正在生成的点,并且能够在它们被点击时消失 moving/animating。请帮帮我!谢谢!
@IBAction func animateButton(sender: AnyObject) {
self.view.userInteractionEnabled = true
// Declare delay counter
var delayCounter:Int = 100000
var durationCounter:Double = 0
// loop for 1000 times
for loopNumber in 0...1000 {
// set up some constants for the animations
let dotDuration:Double = 4 - durationCounter
let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let blueDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let yellowDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let greenDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 25000
let options = UIViewAnimationOptions.AllowUserInteraction
//set up some constants for the dots
let redSize:CGFloat = 54
let redYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let blueSize:CGFloat = 54
let blueYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let yellowSize:CGFloat = 54
let yellowYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
let greenSize:CGFloat = 54
let greenYPosition : CGFloat = CGFloat( arc4random_uniform(275)) + 54
// create the dots and add them to the view
let redDot = UIImageView()
redDot.image = UIImage(named: "Red Dot")
redDot.frame = CGRectMake(0-redSize, redYPosition, redSize, redSize)
self.view.addSubview(redDot)
let blueDot = UIImageView()
blueDot.image = UIImage(named: "Blue Dot")
blueDot.frame = CGRectMake(0-blueSize, blueYPosition, blueSize, blueSize)
self.view.addSubview(blueDot)
let yellowDot = UIImageView()
yellowDot.image = UIImage(named: "Yellow Dot")
yellowDot.frame = CGRectMake(0-yellowSize, yellowYPosition, yellowSize, yellowSize)
self.view.addSubview(yellowDot)
let greenDot = UIImageView()
greenDot.image = UIImage(named: "Green Dot")
greenDot.frame = CGRectMake(0-greenSize, greenYPosition, greenSize, greenSize)
self.view.addSubview(greenDot)
// define the animations
UIView.animateWithDuration(dotDuration , delay: redDelay, options: options, animations: {
redDot.frame = CGRectMake(675, redYPosition, redSize, redSize)
}, completion: { animationFinished in redDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: blueDelay, options: options, animations: {
blueDot.frame = CGRectMake(675, blueYPosition, blueSize, blueSize)
}, completion: { animationFinished in blueDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: yellowDelay, options: options, animations: {
yellowDot.frame = CGRectMake(675, yellowYPosition, yellowSize, yellowSize)
}, completion: { animationFinished in yellowDot.removeFromSuperview() })
UIView.animateWithDuration(dotDuration , delay: greenDelay, options: options, animations: {
greenDot.frame = CGRectMake(675, greenYPosition, greenSize, greenSize)
}, completion: { animationFinished in greenDot.removeFromSuperview() })
// FAILED ATTEMPT
var tapDot:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dotTapped")
// FAILED ATTEMPT
redDot.addGestureRecognizer(tapDot)
// FAILED ATTEMPT
func dotTapped (recognizer: UITapGestureRecognizer) {
redDot.alpha = 0
}
durationCounter+=0.05
delayCounter+=50000
}
}
- 您的
dotTapped
方法应该在您的animateButton
方法之外。 - 确保
userInteractionEnabled
对于UIImageView
是正确的。