2

It is very easy to allow users to interact with views while animating using the options field of block based animation. But in my program I am using a CAKeyframeAnimation and I don't see any properties to set user interaction enabled. Is there any way to do this?

Thanks,

EDIT: Here is the actual code:

- (void)move { CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 50, 120); for (int i = 0; i < 5; i ++) CGPathAddLineToPoint(path, NULL, arc4random() % 320, arc4random() % 480); CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [animation setPath:path]; [animation setDuration:10]; CFRelease(path); [self.layer addAnimation:animation forKey:@"move"]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self setAlpha:0]; } 
4
  • Its a property of the view. You should be able to set it in the mainView. Commented Jun 19, 2012 at 22:48
  • Legolas, I tried that to no avail. Thanks though Commented Jun 19, 2012 at 23:00
  • could you elaborate on what exactly you are trying to achieve ? Commented Jun 19, 2012 at 23:03
  • 1
    I want a small square to appear on the screen. Then using keyframe animations have it move around on the screen. I want the user to be able to touch it and then it will disappear. I'll post my code in the original post Commented Jun 19, 2012 at 23:06

2 Answers 2

1

You are most likely touching the wrong place. When the layer animates on screen the value never changes so the view is actually positioned where it was from the beginning, not where it appears on screen.

I did a blog post a few month ago on how to hit test animating layers. It describes the exact thing that you are trying to do.

You will need to add the touch handling (or gesture recognizer) to the superview and do the hit-testing of the presentationLayer yourself to determine if the user tapped where the view appeared on screen.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David, your article seems to hit exactly the point that I am wondering about. However, I do not want to use a Gesture Recognizer as I want the result directly on the touch down. Is there a way to do that?
Yeah, i agree with David's comment. You are actually not playing with the right coordinates. Why dont you trying displaying the coordinates of where you are touching, and where the cube is, and you can understand a great deal about it.
David, I just realized you implement the gesture recognizer in the vc. That should work for me. Thanks!
0

Apparently, preview solution no longer works in Swift 3.x and Swift 4.x

I solved the issue setting a Timer to update the frame during the animation.

//Timer: To update the UI frame Timer.scheduledTimer(timeInterval: 0.15, target: self, selector: #selector(updateFrame), userInfo: nil, repeats: true) //Timer Selector @objc func updateFrame(){ //getting the layer presentation bounds let layer = "button, label...".layer.presentation()! let point = CGPoint(x: layer.frame.origin.x, y: layer.frame.origin.y) let size = CGSize(width: layer.frame.width, height: layer.frame.height) // Update the UI element frame location "ui element".frame = CGRect(origin: point, size: size) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.