how to to make gesturerecognizer working in an animating UIImage view

*爱你&永不变心* 提交于 2019-12-01 21:44:04

问题


i have 5 animating image in a image view and will like to allow user to tap on them base on the default ID and push it to another view. i tried to add in gesture tap but the imageview are not detecting.

can anybody give me some advise ?

EDIT: end up i did not use it, i set a UIButton instead.

thank you :)

viewDidLoad

self.defaultID = [[NSMutableArray alloc] initWithObjects:@"7",@"9",@"11",@"27",@"6",nil];
self.defaultImageCaption = [[NSMutableArray alloc] initWithObjects:@"Ver",@"Green",@"Red",@"CF",@"Dwarf",nil];

//default image
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;
imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];
imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];

singleDefaultTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDefaultTap:)];
singleDefaultTap.numberOfTouchesRequired = 1;
imageViewTop.userInteractionEnabled = YES;
[imageViewTop addGestureRecognizer:singleDefaultTap];
imageViewTop.tag = 2000;

UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0,44,320,367)];
[topView addSubview:imageViewTop];
[self.view addSubview:imageViewTop];
[self.view addSubview:topView];
[self.view addSubview:imageViewBottom];

[self nextAnimation]

-(void)nextAnimation{

//picture loop
imageViewTop.image = imageViewBottom.image;
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1];

[imageArray insertObject:imageViewBottom.image atIndex:0];
[imageArray removeLastObject];
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;

[UIView animateWithDuration:4.0
                 animations:^{ 
                     imageViewTop.alpha = 0.0;
                     imageViewBottom.alpha = 1.0;
                     } 
                 completion:^(BOOL  completed){
                     [self nextAnimation:stringsize.width];
                 }
 ]; 

action

//show your alert...

NSLog(@"tapped");

flowerDetails = [[FlowerDetailViewController alloc] initWithNibName:@"FlowerDetailViewController" bundle:Nil] ;
Fr *fr = nil;

//push to flower detail view


回答1:


It is because that when animation is applying, the modification for any value of the sprite(UIImageView in your case) by animation is not applied to the sprite original, it is applied to its .layer.presentationLayer, the real one is still at its original place. In this case, you can try this: Put your gesture on the whole screen, when response comes from event, you check the touch point, make a hitTest to the presentationLayer, if yes then do what you want.

I usually use below code to do that.

- ( BOOL ) areYouThere: ( CGPoint ) point {
    return ( [ ( ( CALayer * )sprite.layer.presentationLayer ) hitTest: point ] != nil );
}

Get the touch point from a UITouch object when below method is invoked

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch



回答2:


I would add a transparent view on top of the image view and add a gesture recognizer to that.




回答3:


Really late but I've been facing the same issue and I found what I think is a better solution:

Just use the UIViewAnimationOptionAllowUserInteraction option like that:

[UIView animateWithDuration:4.0
                      delay:0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     imageViewTop.alpha = 0.0;
                     imageViewBottom.alpha = 1.0;
                 } 
                 completion:^(BOOL  completed){
                     [self nextAnimation:stringsize.width];
                 }
];


来源:https://stackoverflow.com/questions/8340329/how-to-to-make-gesturerecognizer-working-in-an-animating-uiimage-view

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