UIImageView在动画期间不响应交互

时间:2013-11-30 23:38:58

标签: ios objective-c animation uiimageview

我正在为几个UIIimageView制作动画,这些UIIimageView附有一个轻击手势识别器。他们在没有动画的情况下做出回应,但是当我做动画时,没有任何回应(用UIButton也试过)。奇怪的是,我在动画中包含了AllowUserInteraction和UIViewAnimationOptionAllowAnimatedContent选项,但它仍然不起作用。我还在iViews上启用了userInteraction并仔细检查以查看viewcontroller视图的boolean值和imageview的viewInteractionEnabled视图以确保它已打开(它是打印出来的1,1)任何想法?

此方法创建图像视图:

-(void)addFloatingButtonHeadToView:(NSString*)userID{

/* Initialize and set properties */
UIImageView *head = [[UIImageView alloc]init];
[head setBackgroundColor:[UIColor clearColor]];
[head.layer setBorderColor:[[UIColor whiteColor]CGColor]];
[head.layer setBorderWidth:3.0f];
[head setUserInteractionEnabled:YES];
head.tag = _floatingButtonHeads.count;

/* Create tap gesture and assign to imageview head */
UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headPressed:)];
[headTap setDelegate:self];
[head addGestureRecognizer:headTap];


/* Add to array and subview, and initialize animation */
[_floatingButtonHeads addObject:head];
[self.view addSubview:head];
[self startFloating:head];

}

此方法启动动画:

-(void)startFloating:(UIImageView*)head{

/* Print values of userInteraction enabled to make sure they are ON */
NSLog(@"%hhd %hhd",self.view.userInteractionEnabled,head.userInteractionEnabled);

/* Create random values for x axis,y axis, and animation duration */
NSUInteger randomX = arc4random_uniform(320) + 60;
NSUInteger randomY = 60 + arc4random() % ((int)self.view.frame.size.height - 60);
NSUInteger randomDuration = arc4random_uniform(4) + 2;

/* Set image view's frame according to values*/
[head setFrame:CGRectMake(randomX, randomY, 60, 60)];

/* Start Animation*/
[UIView animateWithDuration:randomDuration delay:0.0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent animations:^{
    [head setFrame:CGRectMake(350, head.frame.origin.y, 60, 60)];
}completion:^(BOOL finished) {
    if(finished){
        [self startFloating:head];
    }
}];

}

2 个答案:

答案 0 :(得分:2)

在动画期间,任何视图都不会响应触摸事件 - 这是一种预期的行为,因为在大多数情况下,这会导致相当随机的触摸位置并导致糟糕的用户体验。

如果你真的需要这样的行为,我会建议制作一个视图列表来测试命中,并将UITapGestureRecognizer附加到他们的parrent视图中。然后你可以使用

[view.layer.presentationLayer frame] 

属性以在动画期间获取其帧并测试命中的触摸。

答案 1 :(得分:1)

您触摸的动画“视图”是无法接收触摸且没有用户交互属性的CALayer,它只存储图像并制作动画。

您可以在背景视图中处理触摸,这可能是imageview的超级视图或超级视图的超级视图....并测试触摸位置是否在view.layer.presentationLayer的框架中为@PrzemysławWrzesiński说。