0

I am using the following code to generate an image which is touchable.

I read some posts here saying UIButton cannot be used during animation so I changed to use tap gesture. I have also added the UIViewAnimationOptionAllowUserInteraction in option. But I still cannot make it work. May I know the reason and any solution?

hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
hit1.image=[UIImage imageNamed:@"roles.png"];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hitenermy:)];
[hit1 addGestureRecognizer:singleTap];
[hit1 setMultipleTouchEnabled:YES];
[hit1 setUserInteractionEnabled:YES];
hit1.userInteractionEnabled=YES;


[self.view addSubview:hit1];
[hit1 addGestureRecognizer:singleTap];
UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;

[UIView animateWithDuration:15.0
                      delay:1.0
                    options:options
                 animations:^{
                     hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);

                 }
                 completion:^(BOOL finished){

                 }];
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Clarence
  • 1,951
  • 2
  • 34
  • 49
  • what is happening when u tap the imageView? – Dheeraj Singh Dec 22 '14 at 08:15
  • You have to add layer animation & then you can use TapGestureRecognizer to detect tap – Gagan_iOS Dec 22 '14 at 08:17
  • When the view starts, the button appear and started moving. I set the duration to 15sec. But at the first 10sec, there is not response when i touch it. At the end of the animation around last 5 sec, the image responded. – Clarence Dec 22 '14 at 08:19
  • @Clarence why you set twice addGestureRecognizer and setUserInteractionEnabled? – Ilario Dec 22 '14 at 08:23
  • can u show code for hitenermy: method – Dheeraj Singh Dec 22 '14 at 08:25
  • @Gagan_iOS What code should i added to make layer animation? – Clarence Dec 22 '14 at 08:42
  • @DheerajSingh -(void)hitenermy:(UIButton*)sender{ for (LDProgressView *progressView2 in self.progressViews2) { float fullhp =progressView2.progress; float hitone = 0.31; progressView2.progress= fullhp-hitone; } } – Clarence Dec 22 '14 at 08:44
  • Instead of adding animation on object you can add animation on object's layers. Now add tap gesture on your object. This should work. For getting layer, you can simply write: YoursObject.Layer For layer animation you can serach over Apple or Stackoverflow. – Gagan_iOS Dec 22 '14 at 09:02
  • Why you are applying delay in your animation?In animate with duration, you are specifying value 1.0????? [UIView animateWithDuration:15.0 delay:1.0 options:options animations:^{ hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60); } completion:^(BOOL finished){ }]; – NewStackUser Dec 22 '14 at 09:33
  • Try after changing delay from 1 to 0.0. – NewStackUser Dec 22 '14 at 09:35

1 Answers1

1

You can use - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event to get touch event. I have shown for a single imageView but if multiple imageView are present then u can use tag to differentiate them.

you can get more from :UIButton can't be touched while animated with UIView animateWithDuration

- (void)viewDidLoad
    {
        [super viewDidLoad];

        hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
        hit1.image=[UIImage imageNamed:@"roles.png"];

        [self.view addSubview:hit1];

        UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;

        [UIView animateWithDuration:15.0
                              delay:1.0
                            options:options
                         animations:^{
                              hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);
                         }
                         completion:^(BOOL finished){

                         }];
    }



     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {
            UITouch *touch = [touches anyObject];
            CGPoint touchLocation = [touch locationInView:self.view];
            for (UIImageView *imageView in self.view.subviews)
            {
                if ([imageView.layer.presentationLayer hitTest:touchLocation])
                {
                    NSLog(@"Clicked");
                    break;
                }
            }
        }
Community
  • 1
  • 1
Dheeraj Singh
  • 5,143
  • 25
  • 33
  • Sorry, but when i use this code i found another problem. No matter where i touch, any location of the screen, it still detecting the touch. Not just the image. Why this happens?? Thanks – Clarence Dec 22 '14 at 17:18
  • it is because the touch event is for your whole view. – Dheeraj Singh Dec 23 '14 at 04:40
  • Is it possible to specifically event for the image view? – Clarence Dec 23 '14 at 04:42
  • i don't think it would be possible ,but u can use UIView *hitView = [self hitTest:touchLocation withEvent:event]; if([hitView == yourimageview]) then perform actions on your touch on imageview – Dheeraj Singh Dec 23 '14 at 04:46