在动画的UIImageView触摸检测 [英] detecting touch in UIImageView animation
问题描述
我有一个环绕圆形使用路径动画屏幕中的圆形意象。我想,当用户触摸移动圆圈来检测。然而,即使图像移动轮一个连贯的圆圈它的框架仍然是在左上角不动了,我怎么能更新此,这样我可以检测到动态图像上的触摸?这里是code ...
i have a circle image that circles round the screen using a path animation. And i want to detect when the user touches the moving circle. However even though the image is moving round in a continuous circle its frame is still in the top left hand corner not moving, how can i update this so that i can detect a touch on the moving image? Here is the code...
设置动画在viewDidLoad中:
Set Up Animation in ViewDidLoad:
//set up animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 10.0;
pathAnimation.repeatCount = 1000;
CGMutablePathRef curvedPath = CGPathCreateMutable();
//path as a circle
CGRect bounds = CGRectMake(60,170,200,200);
CGPathAddEllipseInRect(curvedPath, NULL, bounds);
//tell animation to use this path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
//add subview
circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
[testView addSubview:circleView];
//animate
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
触动方式:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//detect touch
UITouch *theTouch = [touches anyObject];
//locate and assign touch location
CGPoint startPoint = [theTouch locationInView:self.view];
CGFloat x = startPoint.x;
CGFloat y = startPoint.y;
//create touch point
CGPoint touchPoint = CGPointMake(x, y);
//check to see if the touch is in the rect
if (CGRectContainsPoint(circleView.bounds, touchPoint)) {
NSLog(@"yes");
}
//check image view position
NSLog(@"frame x - %f, y - %f", circleView.frame.origin.x, circleView.frame.origin.y);
NSLog(@"center x - %f, y - %f", circleView.center.x, circleView.center.y);
NSLog(@"bounds x - %f, y - %f", circleView.bounds.origin.x, circleView.bounds.origin.y);
}
在ImageView的似乎只是停留在左上角。我似乎无法弄清楚如何,如果触摸手势已经在运动中的球做出识别。
the imageview just seems to stay at the top left hand corner. I cant seem to figure out how to recognise if a touch gesture has been made on the moving ball.
任何帮助将是AP preciated,
any help would be appreciated,
克里斯
推荐答案
您需要查询视图不是它的框架的presentation层。只有presentation将动画的过程中被更新...
You need to query the presentation layer of the view not it's frame. Only the presentation will be updated during the course of an animation...
[myImageView.layer presentationLayer]
来访问此层(产地,规格等)的属性,确定您的触摸点的范围内。
Access the properties of this layer (origin, size etc) and determine if your touch point is within the bounds.
这篇关于在动画的UIImageView触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!