ฉันมีภาพวงกลมที่วนรอบหน้าจอโดยใช้ภาพเคลื่อนไหวแบบพาธ และฉันต้องการตรวจจับเมื่อผู้ใช้สัมผัสวงกลมที่กำลังเคลื่อนที่ อย่างไรก็ตาม แม้ว่าภาพจะเคลื่อนที่เป็นวงกลมต่อเนื่องกัน แต่กรอบยังคงอยู่ที่มุมซ้ายบนไม่เคลื่อนไหว ฉันจะอัปเดตสิ่งนี้เพื่อตรวจจับการสัมผัสบนภาพเคลื่อนไหวได้อย่างไร นี่คือรหัส...
ตั้งค่าภาพเคลื่อนไหวใน 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 จะอยู่ที่มุมซ้ายบน ดูเหมือนฉันจะไม่รู้ว่าจะรู้ได้อย่างไรว่ามีท่าทางสัมผัสบนลูกบอลที่กำลังเคลื่อนที่หรือไม่
ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม
คริส