如何从imageviewanimation为单击的图像创建动作

时间:2015-06-02 23:13:16

标签: objective-c animation uiimageview uigesturerecognizer

[求助 - 看到答案]

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

如果你知道有办法处理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
}

2 个答案:

答案 0 :(得分:0)

我建议使用hacky解决方案,假设您知道图像数量和动画时间。


 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是您项目的分支,以防您想要使用有效的解决方案。

答案 1 :(得分:0)

您不想使用动画图片视图来执行此操作,因为您在单击图像视图时无法从图像视图中获取图像。而是使用计时器来切换图像。这样的事情应该有用,

filter {
    if [type] == "iis" {
            if [message] =~ "^#" {
                            drop {}
            }
            grok {
                    break_on_match => false
                    match => [
                            "message", "%{TIMESTAMP_ISO8601:log_timestamp} %{IPORHOST:s-sitename} %{IPORHOST:s-ip} %{URIPROTO:cs-method} %{URIPATH:cs-uri-stem} (?:%{NOTSPACE:cs_query}|-) %{NUMBER:src_port} %{NOTSPACE:cs_username} %{IP:clientip} %{NOTSPACE:useragent} %{NUMBER:sc-substatus} %{NUMBER:sc_win32_status} %{NUMBER:sc-bytes} %{NUMBER:cs-bytes} %{NUMBER:timetaken}"
                    ]
            }

            date {

                    locale => "en"
                    match => [ "log_timestamp", "YYYY-MM-dd HH:mm:ss" ]
                    target => "@timestamp"
                    timezone => "Indian/Maldives"
            }
            useragent {
                    source=> "useragent"
                    prefix=> "browser"
            }
            geoip {
                    source => "clientip"
                    target => "geoip"
                    add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
                    add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
            }

            mutate {
                    add_field => [ "src_ip", "%{clientip}" ]
                    convert => [ "[geoip][coordinates]", "float" ]
                    replace => [ "@source_host", "%{clientip}" ]
                    replace => [ "@message", "%{message}" ]
                    rename => [ "cs_method", "method" ]
                    rename => [ "cs_stem", "request" ]
                    rename => [ "useragent", "agent" ]
                    rename => [ "cs_username", "username" ]
                    rename => [ "sc_status", "response" ]
                    rename => [ "timetaken", "time_request" ]
           }
    }
}
filter
  {
    if [type] == "iis" {
            mutate {
                    remove_field => [ "clientip", "host", "hostname", "logtime" ]
            }
    }
}

编辑后

如果您想在图像之间使用不同的时间,我会使用不同的方法。而不是使用计时器,只需调用以下方法来启动动画,

@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
}

这会使你在第2张和第3张图像之间延迟2秒,但在其他图像之间延迟1秒。