การตรวจจับการสัมผัสในภาพเคลื่อนไหว UIImageView

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

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

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม

คริส


person ChrisM    schedule 26.10.2011    source แหล่งที่มา
comment
วิธีที่ง่ายที่สุดวิธีหนึ่งคือการสร้าง UIButton ประเภทกำหนดเองและเพิ่มรูปภาพของคุณเป็นพื้นหลังของปุ่ม จากนั้นคุณสามารถเพิ่มตัวเลือกให้กับปุ่มเพื่อปิดการทำงานเมื่อมีการสัมผัส ฯลฯ หวังว่านี่จะช่วยได้ :)   -  person Luke    schedule 26.10.2011
comment
สวัสดี ใช่ ฉันลองแล้วก่อนอื่นเลย แต่มันก็ไม่ค่อยเหมาะกับสิ่งที่ฉันตามหา เพราะฉันต้องการใช้ touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event เพื่อดูว่าผู้ใช้สามารถถือภาพได้นานแค่ไหน จะต้องมีวิธีเข้าถึงตำแหน่งของภาพอย่างแน่นอน? ไชโย   -  person ChrisM    schedule 26.10.2011


คำตอบ (1)


คุณต้องสอบถามเลเยอร์การนำเสนอของมุมมอง ไม่ใช่เฟรม เฉพาะการนำเสนอเท่านั้นที่จะได้รับการอัปเดตในระหว่างภาพเคลื่อนไหว...

[myImageView.layer presentationLayer]

เข้าถึงคุณสมบัติของเลเยอร์นี้ (ต้นกำเนิด ขนาด ฯลฯ) และตรวจสอบว่าจุดสัมผัสของคุณอยู่ภายในขอบเขตหรือไม่

person Simon Lee    schedule 26.10.2011