1

我有一个动画,它连续在屏幕上运行。现在,当您触摸屏幕某个部分的动画时,我正试图获得警报。

目前我已经到了这样的地步,如果你触摸屏幕中间你会得到一个警报。

SelectedCellViewController.h

#import <Accounts/Accounts.h>
#import <QuartzCore/QuartzCore.h>

@interface SelectedCellViewController : UIViewController {
IBOutlet UIImageView *rocket;
}

@end

SelectedCellViewController.m

#import "SelectedCellViewController.h"

@interface SelectedCellViewController ()

@end

@implementation SelectedCellViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {

[super viewDidLoad];

[self performSelector:@selector(imageSpawn:) withObject:nil afterDelay:3];

  }


     - (void) imageSpawn:(id) sender
   {

      UIImage* image = [UIImage imageNamed:@"ae"];
      rocket = [[UIImageView alloc] initWithImage:image];
       rocket.frame = CGRectMake(-25, 200, 25, 40);
       [UIView animateWithDuration:5
                      delay:0.2f
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
                 completion:^(BOOL fin) {
                 }];

   UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];
[self.view setUserInteractionEnabled:YES];
[self.view addSubview:rocket];

}

-(void)ballTapped:(UIGestureRecognizer *)gesture
{

//  [rocket.layer removeAllAnimations];
NSLog(@"Tag = %d", gesture.view.tag);

 CGPoint location = [gesture locationInView:gesture.view];

Rocket.frame = CGRectMake(location.x,location.y,25,40);

 CGPoint location1 = [gesture locationInView:gesture.view];
CGRect rectToCompare =  CGRectMake(10.0f, 200.0f, 200.0f, 42.0f);

if (CGRectContainsPoint(rectToCompare, location1)) {

    //trigger an event.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tapped row!"
                                                    message:[NSString stringWithFormat:@"Shot"]
                                                   delegate:nil
                                          cancelButtonTitle:@"Yes, I did!"
                                          otherButtonTitles:nil];
    [alert show];
}

}

- (void)viewDidUnload {

}

@end 
4

1 回答 1

1

要在动画中间获取视图属性的值,您必须使用视图层的presentationLayer. 正如文档所述

此方法返回的图层对象提供了当前显示在屏幕上的图层的近似值。当动画正在进行时,您可以检索此对象并使用它来获取这些动画的当前值。

因此,您可以使用view.layer.presentationLayer.framewhere viewis yourUIImageView来获取屏幕上动画层的当前位置。使用它,测试框架是否在您想要检测的边界内是微不足道的。

于 2013-03-30T17:14:00.727 回答