我正在尝试创建自定义活动指示器它有两个图像一个图像将是静态的(在后台)而另一个图像将在前面,它必须向左,向右,向上和向下移动(只是为了给动画感觉)。我试着使用下面的代码(错误的代码)。任何人都可以帮助我,如何做动画
UIImage *statusImage = [UIImage imageNamed:@"image1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"], nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 7;
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/3,
self.view.frame.size.height/2
-statusImage.size.height/3,
statusImage.size.width/3,
statusImage.size.height/3);
//Start the animation
[activityImageView startAnimating];
答案 0 :(得分:0)
UIImageView可以通过图像制作动画,有点像gif。你是什么 想要使用CAAnimation可以更优雅地实现。这段代码给出了向左和向右的动画:
- (CATransform3D) moveToRight
{
return [self moveWithScale:CGSizeMake(0.25f, 1.5f)];
}
- (CATransform3D) moveToLeft
{
return [self moveWithScale: CGSizeMake(-0.25f, 1.5f)];
}
- (CATransform3D) moveWithScale: (CGSize) scale
{
CGAffineTransform scaleTransform CGAffineTransformMakeScale(ABS(scale.width), scale.height);
CGFloat XTranslationFactor = (1.0f - ABS(scale.width)) / 2.0 * (ABS(scale.width) / scale.width);
CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(XTranslationFactor * self.bounds.size.width,
0.0f);
CGAffineTransform completeTransform = CGAffineTransformConcat(scaleTransform, translationTransform);
CATransform3D transform3D = CATransform3DMakeAffineTransform(completeTransform);
return transform3D;
}
- (NSArray *) keyFrameAnimationValues
{
values = @[[NSValue valueWithCATransform3D: [self moveToLeft]],
[NSValue valueWithCATransform3D: [self moveToRight]],
[NSValue valueWithCATransform3D: [self moveToTop]],
[NSValue valueWithCATransform3D: [self moveToBottle]]
];
}
- (void) startAnimating
{
NSArray * values = [self keyFrameAnimationValues];
NSInteger frameCount = frameValues.count;
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath: @"transform"];
keyFrameAnimation.duration = 7;
keyFrameAnimation.calculationMode = kCAAnimationCubic;
keyFrameAnimation.values = [self keyFrameAnimationValues];
// this next part could be done a little more generic
// by constructing the array dynamically using a for loop,
// but this will give you an idea
keyFrameAnimation.keyTimes = @[@(1.0f / frameCount),
@(2.0f / frameCount),
@(3.0f / frameCount),
@(4.0f / frameCount)
];
keyFrameAnimation.repeatCount = HUGE_VALF;
[self.layer addAnimation: keyFrameAnimation forKey: @"myAnimation"];
}
答案 1 :(得分:0)
为要在每个角上移动的图层创建CAAnimation,并将其添加为定位对象。希望它有所帮助