如何在更改照片时设置UIImageView缩放动画

时间:2015-03-22 16:13:48

标签: ios objective-c animation uiimageview zoom

作为头衔,我是初学者,所以我没有想法解决它。

在我的代码中,当我第一次打开模拟器时,它只有缩放动画,

我希望每张照片都有变焦动画,

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,300,300)];
    imageView.animationImages = animationImages ;
    imageView.animationRepeatCount = 0;
    imageView.animationDuration= 12.0;
    [imageView startAnimating];
    [self.view addSubview:imageView];

    imageView.frame = CGRectMake(150, 150, 20, 20);
    CGPoint center = imageView.center;
    [UIView animateWithDuration: 1.0f animations:^{
        imageView.frame = CGRectMake(10, 10, 300, 300);
        imageView.center = center;
    }];
}

提前感谢您提供此处提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您想在每个图像转换上使用缩放动画,则不是使用animationImages的最干净的代码。我会创建自己的自定义UIView类来自己处理转换,因此动画可以与每个转换紧密同步。但是,为了通过快速不可靠的黑客回答您的问题,我将使用计时器以与图像转换大致相同的频率运行动画。定时器随着时间的推移变得不准确,因此最终缩放效果不再与图像过渡对齐。

@interface NameOfYourViewController ()

@property(nonatomic, strong) UIImageView* imageView;
@property(nonatomic, strong) NSTimer* imageTransitionTimer;
@property(nonatomic, assign) CGFloat imageScale;
- (void)animateImage;

@end

@implementation NameOfYourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     NSArray *animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
                                [UIImage imageNamed:@"2.jpg"],
                                [UIImage imageNamed:@"3.jpg"],
                                [UIImage imageNamed:@"4.jpg"],nil];

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,300,300)];
    selfimageView.animationImages = animationImages ;
    self.imageView.animationRepeatCount = 0;
    self.imageView.animationDuration= 12.0;
    [self.imageView startAnimating];
    [self.view addSubview:self.imageView];

    self.imageScale = 1.0f;
    self.imageTransitionTimer = [NSTimer scheduledTimerWithTimeInterval:imageView.animationDuration target:self selector:@selector(animateImage) userInfo:nil repeats:YES];
    [self.imageTransitionTimer fire];
}

- (void)animateImage
{
   [UIView 
     animateWithDuration:1.0 
     delay:0.0
     options:0
     animations:^void () {
       self.imageView.transform = CGAffineTransformMakeScale(
         self.imageScale,
         self.imageScale 
       );
     }
     completion:^void(BOOL finished) {
       self.imageScale += 0.3f;
     }
   }]; 
}

@end