Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何从imageviewanimation为单击的图像创建动作 [已解决-请参阅答案]_Objective C_Animation_Uiimageview_Uigesturerecognizer - Fatal编程技术网

Objective c 如何从imageviewanimation为单击的图像创建动作 [已解决-请参阅答案]

Objective c 如何从imageviewanimation为单击的图像创建动作 [已解决-请参阅答案],objective-c,animation,uiimageview,uigesturerecognizer,Objective C,Animation,Uiimageview,Uigesturerecognizer,我想将三个图像分组为动画,我可以单击这三个图像中的任何一个来定义动作。换句话说,我想知道如何为动画中的每个图像创建单独的动作/事件 如果您知道有一种使用UIButton而不是UIImageview的方法,那就太好了 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Load images NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.pn

我想将三个图像分组为动画,我可以单击这三个图像中的任何一个来定义动作。换句话说,我想知道如何为动画中的每个图像创建单独的动作/事件

如果您知道有一种使用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
}
-(void)viewDidLoad{
[超级视图下载];
//加载图像
NSArray*imageNames=@[@“hello.png”、@“bye.png”、@“helloagain.png”];
NSMutableArray*图像=[[NSMutableArray alloc]init];
对于(int i=0;i
假设您知道图像数量和动画时间,我建议的黑客解决方案


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);
}
-(无效)设置{
//加载图像
NSArray*imageNames=@[@“快乐”、“悲伤”、“大力水手村”];
NSMutableArray*图像=[[NSMutableArray alloc]init];
对于(int i=0;i
虽然与上述解决方案非常相似,但仍使用UIImageView的animationImages。

如果你想使用一个有效的解决方案,这是我的项目分支。

假设你知道图像数量和动画时间,我建议的黑客解决方案


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);
}
-(无效)设置{
//加载图像
NSArray*imageNames=@[@“快乐”、“悲伤”、“大力水手村”];
NSMutableArray*图像=[[NSMutableArray alloc]init];
对于(int i=0;i
虽然与上述解决方案非常相似,但仍使用UIImageView的animationImages。

是我的项目分支,以防您想使用工作解决方案。

您不想使用动画图像视图来执行此操作,因为您需要
-(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];
    }
}