本例实现一个碰撞的火球,当火球碰到界面后就会实现反弹的动作,并且火球的阴影会慢慢的变小以及消失。
#import "ViewController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
static const CGFloat kBallRadius = 15.0f;
static const CGFloat kShowOffset = 10.0f;
@interface ViewController ()
@property (nonatomic, strong) UIImageView *fireBall;
@property (nonatomic) CGPoint point;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.fireBall];
// 初始化每次移动的位置
self.point = CGPointMake(kBallRadius, kBallRadius/2.0);
// Moving
[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onMovingTimer) userInfo:nil repeats:YES];
}
-(void)onMovingTimer {
CGFloat x = self.fireBall.center.x;
CGFloat y = self.fireBall.center.y;
// 改变其位置
self.fireBall.center = CGPointMake(self.fireBall.center.x + self.point.x, self.fireBall.center.y + self.point.y);
// 判断当其遇到边境的时候
CGPoint tempPoint = self.point;
if (self.fireBall.center.x > SCREEN_WIDTH - kBallRadius || self.fireBall.center.x < kBallRadius) {
tempPoint.x = - self.point.x;
self.point = tempPoint;
}
if (self.fireBall.center.y > SCREEN_HEIGHT - kBallRadius || self.fireBall.center.y < kBallRadius) {
tempPoint.y = - self.point.y;
self.point = tempPoint;
}
// 变成灰色的,其实就是后面的跟随者
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grayBall"]];
imageView.frame = CGRectMake(x - kBallRadius, y - kBallRadius, kBallRadius * 2 - 2, kBallRadius * 2 - 2);
[self.view addSubview:imageView];
// 让跟随者不断变化,造成越来越小,并且消失
[UIView animateWithDuration:3.0f delay:0.f options:UIViewAnimationOptionCurveEaseOut animations:^{
// 位置,大小,透明度
imageView.frame = CGRectMake(x - kBallRadius + kShowOffset, y - kBallRadius + kShowOffset, kBallRadius - 4, kBallRadius - 4);
[imageView setAlpha:0.0];
} completion:^(BOOL finished) {
[imageView removeFromSuperview];
}];
// 让 FireBall 在上面
[self.view bringSubviewToFront:self.fireBall];
}
- (UIImageView *)fireBall {
if (!_fireBall) {
_fireBall = [[UIImageView alloc] init];
_fireBall.image = [UIImage imageNamed:@"hotBall"];
_fireBall.frame = CGRectMake(0, 0, kBallRadius * 2, kBallRadius * 2);
}
return _fireBall;
}
@end
注意点 UIView animateWithDuration: 这几个方法
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
* duration: 表示动画执行时间。
* delay: 动画延迟时间。
* usingSpringWithDamping: 表示弹性属性。
* initialSpringVelocity: 初速度。
* options :可选项,一些可选的动画效果,包括重复等。
* animations: 表示执行的动画内容,包括透明度的渐变,移动,缩放。
* completion: 表示执行完动画后执行的内容。
此处对 option 中进行备注下:
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
//提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionLayoutSubviews = 1 << 0,
//动画时允许用户交流,比如触摸
UIViewAnimationOptionAllowUserInteraction = 1 << 1,
//从当前状态开始动画
UIViewAnimationOptionBeginFromCurrentState = 1 << 2,
// 动画重复
UIViewAnimationOptionRepeat = 1 << 3,
// 执行动画回路,前提是设置动画无限重复
UIViewAnimationOptionAutoreverse = 1 << 4,
//忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5,
//忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6,
// 通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionAllowAnimatedContent = 1 << 7,
//用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionShowHideTransitionViews = 1 << 8,
//忽略嵌套继承的�选项
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9,
//***时间函数曲线相关**//
//时间曲线函数,由慢到快
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
// 时间曲线函数,由慢到特别快
UIViewAnimationOptionCurveEaseIn = 1 << 16,
// 时间曲线函数,由快到慢
UIViewAnimationOptionCurveEaseOut = 2 << 16,
// 时间曲线函数,匀速
UIViewAnimationOptionCurveLinear = 3 << 16,
// ** 转场相关的 **//
// 无转场动画
UIViewAnimationOptionTransitionNone = 0 << 20, // default
// 转场从左翻转
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
// 上卷转场
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
// 转场交叉消失
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
// 转场从上翻转
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
} NS_ENUM_AVAILABLE_IOS(4_0);
此处 要注意是 与 时间函数 相关的几个 Options, 然后例举本个动画案例如何让其速度变化才是最真实呢?这个动画主要还是进一步了解 UIView 的几个动画方法,以及告诉自己动画没有那么难的。
PS:场景来自:【iOS开发范例实战宝典.进阶篇】。