簡體   English   中英

UIImage動畫一系列圖像

[英]UIImage animating an array of images

- (IBAction)didTouchAnimate:(UIButton*)sender {

    UIImage *starImage = [UIImage imageNamed:@"star.png"];
    UIImageView *starView = [[UIImageView alloc] initWithImage:starImage];
    [starView setCenter:sender.center];

    [UIView animateWithDuration:1.50f delay:0.0f options:UIViewAnimationCurveEaseInOutanimations:^{
                         [starView setCenter:CGPointMake(+100, +100)];
                         [starView setAlpha:0.6f];
                     }
                     completion:^(BOOL finished){
                         [starView removeFromSuperview];
                         // points++;
                         // NSLog(@"points: %i", points);
                     }];

    [self.view addSubview:starView];
}

我有一個數組列表。 如何為這些圖像添加動畫效果。

Dollars.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"score_animate_0001.png"],
[UIImage imageNamed:@"score_animate_0002.png"],
[UIImage imageNamed:@"score_animate_0003.png"],
[UIImage imageNamed:@"score_animate_0004.png"],
[UIImage imageNamed:@"score_animate_0005.png"],
[UIImage imageNamed:@"score_animate_0006.png"],
[UIImage imageNamed:@"score_animate_0007.png"],nil];

本教程將幫助您......

UIImageView動畫教程

否則,在didTouchAnimate方法中執行此didTouchAnimate

UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200,200)];

testArray  = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_001.png"],    [UIImage imageNamed:@"test_002.png"],[UIImage imageNamed:@"test_001.png"],nil];
animTime =3.0;
[animatedImageView setAnimationImages:testArray] ;
animatedImageView.animationDuration =  animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];

采用

- (IBAction)didTouchAnimate:(UIButton*)sender {

  UIImageView *animatingImageView = [[UIImageView alloc] initWithFrame:some.frame];
  animatingImageView.animationImages = Dollars.animationImages;
  animatingImageView.animationDuration = 10;
  [animatingImageView startAnimating];

or simply 
[animatingImageView animatedImagesWithImages:Dollars.animationImages duration:10]
}

采用

[starView animatedImagesWithImages:Dollars.animationImages duration:10]

Swift 3.0

這是具有自定義樣式的動畫:

extension UIImageView {
    func animate(images: [UIImage], index: Int = 0, duration: TimeInterval, completionHandler: (() -> Void)?) {
        UIView.transition(with: self, duration: duration / Double(images.count), options: .transitionCrossDissolve, animations: {
            self.image = images[index]
        }, completion: { value in
            let idx = index == images.count-1 ? 0 : index+1
            if idx == 0 {
                completionHandler!()
            } else {
                self.animate(images: images, index: idx, duration: duration / Double(images.count),completionHandler: completionHandler)
            }
        })
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM