ios uiimageview tap gesture not working during animation

ⅰ亾dé卋堺 提交于 2019-12-11 06:59:05

问题


I have a UIImageView that is animated and receiving a UITapGestureRecognizer. The problem is while animated, the tap gesture area doesn't move with the view. I can tap the view's original location and the tap gesture is recognized. Anyone know how to have the tap recognized while the view is moving and not at its original position?

Edit 1: This is how I've set up my tap gesture recognizer:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapRecognizer.cancelsTouchesInView = YES;
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.delegate = (id)self;
    [imageView addGestureRecognizer:tapRecognizer];
    imageView.userInteractionEnabled = YES;

Is there a way to have the gesture recognizer follow my view on its path since my animation lasts a couple seconds? Have I missed a setting?

Edit 2: This is my code to set up my animation:

    double width = backgroundImageView.frame.size.width;
    double height = backgroundImageView.frame.size.height;

    CGMutablePathRef path = CGPathCreateMutable();
    double startX = -imageView.frame.size.width;
    double startY = height/4;
    double endX = 3*width;
    double endY = 0;
    double duration = 40;
    CGPathMoveToPoint(path, NULL, startX, startY);
    CGPathAddLineToPoint(path, NULL, endX, endY);
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = duration;
    animation.path = path;
    animation.repeatCount = HUGE_VALF;
    [imageView.layer addAnimation:animation forKey:@"theAnimation"];

回答1:


For anyone interested, I solved my problem. Simply use an NSTimer, check and update the UIImageView's frame from the presentation layer by:

CGRect viewLocation = [[[imageView layer] presentationLayer] frame];    
imageView.frame = CGRectMake(viewLocation.origin.x, viewLocation.origin.y, viewLocation.size.width, viewLocation.size.height);

After that, touches work wherever my image view is :-)



来源:https://stackoverflow.com/questions/12287267/ios-uiimageview-tap-gesture-not-working-during-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!