如何从 imageview 动画中为点击的图像创建动作
how to create an action for a clicked image from imageviewanimation
[已解决 - 查看答案]
我想将三个图像分组为动画,并且我可以单击这三个图像中的任何一个来定义一个动作。换句话说,我想知道为动画中的每个图像创建单独的动作/事件的方法
如果您知道有一种方法可以使用 UIButton 而不是 UIImageview,那就太好了
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Load images
NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 1.5;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
animationImageView.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[animationImageView addGestureRecognizer:singleTap];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
//action here
}
假设您知道图像数量和动画时间,我提出的骇人听闻的解决方案。
1. 安排 imageCount/animationTime 毫秒的计时器。
2. 在计时器的回调中,在 UIImageView
上增加上面的 属性
3. 在 -bannerTapped
中使用该索引来决定要抛出与当前动画图像相对应的事件。
-(void)setup {
// Load images
NSArray *imageNames = @[@"happy",@"sad",@"popeye_village"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
imageview.frame = CGRectMake(60, 95, 86, 90);
imageview.animationImages = images;
imageview.animationDuration = 6;
[imageview startAnimating];
imageview.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[imageview addGestureRecognizer:singleTap];
[NSTimer scheduledTimerWithTimeInterval:((double)imageview.animationDuration / images.count)
target:self
selector:@selector(updateAnimatingImage)
userInfo:nil
repeats:YES];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Clicked on Image index %li", imageview.indexOfAnimatingImage] message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
}
-(void)updateAnimatingImage
{
imageview.indexOfAnimatingImage = ++imageview.indexOfAnimatingImage % (imageview.animationImages.count);
NSLog(@"index currently showing is %li",imageview.indexOfAnimatingImage);
}
虽然与上述解决方案非常相似,但它仍然使用 UIImageView 的 animationImages。
Here 是我对你的项目的分支,以防你想尝试一个可行的解决方案。
您不想使用动画图像视图来执行此操作,因为当您单击它时无法从图像视图中获取图像。相反,使用定时器来切换图像。这样的东西应该可以工作,
@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *images;
@property (strong,nonatomic) UIImageView *animationImageView;
@end
@implementation ViewController {
NSInteger counter;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png"];
self.images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
self.animationImageView.image = self.images[0];
[self.view addSubview:self.animationImageView];
self.animationImageView.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.animationImageView addGestureRecognizer:singleTap];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
}
-(void)changeImage:(NSTimer *) timer {
if (counter == self.images.count - 1 ) {
counter = 0;
}else {
counter ++;
}
self.animationImageView.image = self.images[counter];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
UIImage *img = self.animationImageView.image;
NSInteger indx = [self.images indexOfObject:img];
NSLog(@"The index is: %ld", indx);
// use indx value in a switch statement or if-else block to choose your action
}
编辑后
如果您希望图像之间的时间不同,我会使用不同的方法。不用定时器,直接调用下面的方法启动动画,
-(void)changeImage {
if (counter == self.images.count - 1 ) {
counter = 0;
}else {
counter ++;
}
self.animationImageView.image = self.images[counter];
if (counter == 1) {
[self performSelector:@selector(changeImage) withObject:nil afterDelay:2];
}else{
[self performSelector:@selector(changeImage) withObject:nil afterDelay:1];
}
}
这将使您在第 2 和第 3 个图像之间有 2 秒的延迟,但在其他图像之间有 1 秒的延迟。
[已解决 - 查看答案]
我想将三个图像分组为动画,并且我可以单击这三个图像中的任何一个来定义一个动作。换句话说,我想知道为动画中的每个图像创建单独的动作/事件的方法
如果您知道有一种方法可以使用 UIButton 而不是 UIImageview,那就太好了
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Load images
NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 1.5;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
animationImageView.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[animationImageView addGestureRecognizer:singleTap];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
//action here
}
假设您知道图像数量和动画时间,我提出的骇人听闻的解决方案。
1. 安排 imageCount/animationTime 毫秒的计时器。
2. 在计时器的回调中,在 UIImageView
上增加上面的 属性
3. 在 -bannerTapped
中使用该索引来决定要抛出与当前动画图像相对应的事件。
-(void)setup {
// Load images
NSArray *imageNames = @[@"happy",@"sad",@"popeye_village"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
imageview.frame = CGRectMake(60, 95, 86, 90);
imageview.animationImages = images;
imageview.animationDuration = 6;
[imageview startAnimating];
imageview.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[imageview addGestureRecognizer:singleTap];
[NSTimer scheduledTimerWithTimeInterval:((double)imageview.animationDuration / images.count)
target:self
selector:@selector(updateAnimatingImage)
userInfo:nil
repeats:YES];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Clicked on Image index %li", imageview.indexOfAnimatingImage] message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
}
-(void)updateAnimatingImage
{
imageview.indexOfAnimatingImage = ++imageview.indexOfAnimatingImage % (imageview.animationImages.count);
NSLog(@"index currently showing is %li",imageview.indexOfAnimatingImage);
}
虽然与上述解决方案非常相似,但它仍然使用 UIImageView 的 animationImages。
Here 是我对你的项目的分支,以防你想尝试一个可行的解决方案。
您不想使用动画图像视图来执行此操作,因为当您单击它时无法从图像视图中获取图像。相反,使用定时器来切换图像。这样的东西应该可以工作,
@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *images;
@property (strong,nonatomic) UIImageView *animationImageView;
@end
@implementation ViewController {
NSInteger counter;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png"];
self.images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
self.animationImageView.image = self.images[0];
[self.view addSubview:self.animationImageView];
self.animationImageView.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.animationImageView addGestureRecognizer:singleTap];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
}
-(void)changeImage:(NSTimer *) timer {
if (counter == self.images.count - 1 ) {
counter = 0;
}else {
counter ++;
}
self.animationImageView.image = self.images[counter];
}
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
UIImage *img = self.animationImageView.image;
NSInteger indx = [self.images indexOfObject:img];
NSLog(@"The index is: %ld", indx);
// use indx value in a switch statement or if-else block to choose your action
}
编辑后
如果您希望图像之间的时间不同,我会使用不同的方法。不用定时器,直接调用下面的方法启动动画,
-(void)changeImage {
if (counter == self.images.count - 1 ) {
counter = 0;
}else {
counter ++;
}
self.animationImageView.image = self.images[counter];
if (counter == 1) {
[self performSelector:@selector(changeImage) withObject:nil afterDelay:2];
}else{
[self performSelector:@selector(changeImage) withObject:nil afterDelay:1];
}
}
这将使您在第 2 和第 3 个图像之间有 2 秒的延迟,但在其他图像之间有 1 秒的延迟。