// 1.5s后自动调用self的hideHUD方法
[self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 1.5s后自动执行这个block里面的代码
self.hud.alpha = 0.0;
});
// 1.5s后自动调用self的hideHUD方法
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];
// repeats如果为YES,意味着每隔1.5s都会调用一次self的hidHUD方法
NSTimer「定时器」
作用:
使用:
// 开启一个定时器任务
// 每隔 ti 秒,调用 1次 aTarget 的 aSelector方法,yesOrNo 是否重复调用 aSelector方法
+(NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInerval)ti target:(id)aTarget selector:(SEL)aSelector userInfor:(id)userInfor repeats:(BOOL)yesOrNo;
// 停止定时器工作
// 一旦定时器停止,不能再次执行任务
// 只能创建新的定时器,执行新任务
- (void)invalidate;
[self.timer invalidate];
self.timer = nil;
// 解决定时器在主线不工作问题
NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(functionName) userInfor:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
简介
CG 作为前缀作用
简介
作用
种类
- (void)drawRect:(CGRect)rect 方法图形上下文 将绘制的所有内容渲染显示到 view 上面
具体操作
- (void)drawRect:(CGRect)rect {
// 0. 获取上下文 CGContextRef 是结构体,这里面已表明是指针,所以是ctx而非*ctx
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 1. 描述路径
// 设置 起点
CGContextMoveToPoint(ctx, 50, 50);
// 设置 下一个点的位置,并和上一个点连线
CGContextAddLineToPoint(ctx, 200, 200);
// 2. 渲染上下文,stroke 空心的形式显示「圆圈、线段都属于空心」
CGContextStrokePath(ctx);
}
作用
图形上下文 将绘制的所有内容渲染显示到 view 上面
何时 drawRect 被调用
setNeedsDisplay 或者 setNeedsDisplayInRect: 时简介:将当前的上下文copy一份,保存到栈顶「那个栈叫做图形上下文栈」
作用
代码示例
- (void)drawRect:(CGRect)rect
{
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 1. 将ctx拷贝一份放到栈中
CGContextSaveGState(ctx);
// 设置绘图状态
CGContextSetLineWidth(ctx, 10);
[[UIColor redColor] set];
CGContextSetLineCap(ctx, kCGLineCapRound);
// 第1根线
CGContextMoveToPoint(ctx, 50, 50);
CGContextAddLineToPoint(ctx, 120, 190);
// 将绘制的图形渲染到上下文中
CGContextStrokePath(ctx);
// 2. 将栈顶的上下文出栈,替换当前的上下文
CGContextRestoreGState(ctx);
// 第2根线
CGContextMoveToPoint(ctx, 10, 70);
CGContextAddLineToPoint(ctx, 220, 290);
CGContextStrokePath(ctx);
// 以下代码和 CGContextStrokePath 效果一样,但更加通用
CGContextDrawPath(ctx, kCGPathStroke);
}
简介
@property(nonatomic,readonly,retain) CALayer *layer;
作用
属性
// 边框宽度
@property CGFloat borderWidth;
// 圆角半径
@property CGFloat cornerRadius;
// 宽度和高度
@property CGRect bounds;
// 位置:坐标系是 父layer的坐标系,决定了本类中 anchorPoint点的位置在父layer坐标系的位置
@property CGPoint position;
// 锚点: 坐标系是layer自己的坐标系,决定了layer绕哪个点旋转「x,y的范围都是 0 ~ 1」
@property CGPoint anchorPoint;
// 边框颜色「CGColorRef类型」
@property CGColorRef borderColor;
// 内容「比如设置为图片CGImageRef」
@property(retain) id contents;
// 背景颜色「CGColorRef类型」
@property CGColorRef backgroundColor;
// 形变属性
@property CATransform3D transform;
方法
// CATransform3DMakeRotation(旋转角度, x, y, z)
// 旋转的轴心是 原点到传入的点(x, y, z) 的连线
self.iconView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1);
// 可以传递哪些key path, 在官方文档搜索 "CATransform3D key paths"
[self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
框架的归属不同
QuartzCore 框架 中的CoreGraphics 框架 中的UIKit 框架 中的框架的使用范围不同
UIKit 框架 只能在 iOS 中使用QuartzCore 框架 和 CoreGraphics 框架 是可以跨平台在 iOS 和 Mac OS X 上使用为了保证可移植性
/**
* CG:Core Graphics框架
* CA:Core Animation框架
* Ref:引用
* 转换:UIKit 框架转换为 CG框架的东西,直接 .CG** 就可以
* 图片一共三层
* 1. 本类的层,用来接收触摸手势
* 2. UIImageView 绘制层
* 3. UIImage 变化层
*/
// 新建图层
// 方法1:[[CALayer alloc] init];
// 方法2:[CALayer layer];
CALayer *layer = [CALayer layer];
// UIKit 框架转换为 CG框架的 CGColor
layer.backgroundColor = [UIColor redColor].CGColor;
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.position = CGPointMake(200, 100);
layer.cornerRadius = 10;
// 将画的形状剪裁掉
layer.masksToBounds = YES;
// UIKit 框架转换为 CG框架的 CGImage
// contents 的属性类型是 id,所以这里要进行强行转换
layer.contents = (id)[UIImage imageNamed:@"catImgName"].CGImage;
[self.view.layer addSublayer:layer];
性能比较
结构比较
每一个 UIView 内部都默认关联着一个 CALayer,这个 Layer 称为 Root Layer(根层)
所有的非 Root Layer,也就是手动创建的 CALayer 对象,都存在着隐式动画
简介
常见的 Animatable Properties「可动画属性」列举
Animatable 注释的都可以做隐式动画通过动画事务开关隐式动画
// 开启 动画事务
[CATransaction begin];
// 隐式动画效果关闭
[CATransaction setDisableActions:YES];
self.testView.layer.position = CGPointMake(10, 10); // 没有隐式动画效果了
// 关闭 动画事务
[CATransaction commit];
步骤
drawInContext: 方法Quartz2D API 进行绘图注意
setNeedsDisplay 这个方法,才会触发 drawInContext: 方法的调用,然后进行绘图MyLayer *layer = [MyLayer layer];
// 设置层的宽高
layer.bounds = CGRectMake(0, 0, 100, 100);
// 设置层的位置
layer.position = CGPointMake(100, 100);
// 开始绘制图层
[layer setNeedsDisplay];
// 添加子层
[self.view.layer addSublayer:layer];
步骤
drawLayer:inContext: 方法drawLayer:inContext: 方法进行绘图drawLayer:inContext: 是 NSObject 对象都有的方法
注意
// 创建图层
CALayer *layer = [CALayer layer];
// 设置 delegate,self 指控制器
// 这里的 delegate 不需要遵守协议,因为这里的代理方法drawLayer:inContext:是 NSObject 对象都有的方法
layer.delegate = self;
// 设置层的宽高
layer.bounds = CGRectMake(0, 0, 100, 100);
// 设置层的位置
layer.position = CGPointMake(100, 100);
// 开始绘制图层
// 调用这个方法,才会通知 delegate 进行绘图
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
view 内部的 Layer 会准备好一个 CGContextRef「图层类型上下文」
调用 delegate「这里就是 UIView」的 drawLayer:inContext: 方法
传入已经准备好的 CGContextRef 对象
view 在 drawLayer:inContext: 方法中又会调用自己的 drawRect: 方法
view 就可以在 drawRect: 方法中实现绘图代码, 所有东西最终都绘制到 view.layer 上面
系统再将 view.layer 的内容拷贝到屏幕, 于是完成了 view 的显示
由 layer 传入的 CGContextRef 对象
drawRect: 中通过 UIGraphicsGetCurrentContext() 获取的上下文drawRect: 中完成的所有绘图都会填入层的 CGContextRef 中,然后被拷贝至屏幕Core Animation 框架简介
使用步骤
addAnimation:forKey: 方法增加 CAAnimation 对象到 CALayer 中,这样就能开始执行动画了removeAnimationForKey: 方法可以停止 CALayer 中的动画想执行动画,就必须初始化一个 CAAnimation 对象
CAAnimation 的继承结构
duration:动画的持续时间repeatCount:动画的重复次数repeatDuration:动画的重复时间fillMode:决定当前对象在 非 active 时间段的行为beginTime:可以用来设置动画延迟执行时间CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间fillMode 属性值
要想 fillMode 有效,最好设置 removedOnCompletion = NO
kCAFillModeRemoved「默认值」kCAFillModeForwardskCAFillModeBackwardskCAFillModeBoth父图层 和 图层本地的时间 换算公式
t = (tp - beginTime) * speed + timeOffsetbeginTime = tp - (t - timeOffset)/speedt:active local time「图层的本地时间」tp:parent layer time「父图层的时间」CALayer 上动画的暂停和恢复
// 暂停 CALayer 的动画
- (void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0; // 让CALayer的时间停止走动
layer.timeOffset = pausedTime; // 让CALayer的时间停留在pausedTime这个时刻
}
// 恢复 CALayer 的动画
- (void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = layer.timeOffset;
layer.speed = 1.0; // 让CALayer的时间继续行走
layer.timeOffset = 0.0; // 取消上次记录的停留时刻
layer.beginTime = 0.0; // 取消上次设置的时间
// 计算暂停的时间「用 CACurrentMediaTime() - pausedTime 也一样」
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
// 设置相对于父坐标系的开始时间「往后退timeSincePause」
layer.beginTime = timeSincePause;
}
removedOnCompletion:默认为 YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。timingFunction:速度控制函数,控制动画运行的节奏delegate:动画代理,用来监听动画的执行过程timingFunction 可选的值有:
kCAMediaTimingFunctionLinear //「线性」:匀速,给你一个相对静态的感觉
kCAMediaTimingFunctionEaseIn //「渐进」:动画缓慢进入,然后加速离开
kCAMediaTimingFunctionEaseOut //「渐出」:动画全速进入,然后减速的到达目的地
kCAMediaTimingFunctionEaseInEaseOut //「渐进渐出」:动画缓慢的进入,中间加速,然后减速的到达目的地「默认」
代理需要实现的方法有:
// 动画开始执行的时候触发这个方法
- (void)animationDidStart:(CAAnimation *)anim;
// 动画执行完毕的时候触发这个方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
注意:以上所有属性和方法都属于 CAAnimation 抽象类,所以它的子类都能使用
简介
CABasicAnimation 和 CAKeyframeAnimation
简介
示例:平移动画
// 1. 说明这个动画对象要对 CALayer的position属性 执行动画
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
// fromValue 到 toValue 的动画持续 1.5s
anim.duration = 1.5;
// position属性值从(50, 80)渐变到(300, 350)
// fromValue 默认是当前的值
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 80)];
// toValue:达到多少值
// byValue:增加多少值
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 350)];
// 设置动画的代理「self指控制器」
anim.delegate = self;
// 2. 让图层保持动画执行后的状态「这里以下两行代码虽然这么保持了状态,但实质上图层的属性值没有改变」
// 动画执行完毕后不删除动画
anim.removedOnCompletion = NO; // 默认是 YES
// 保持最新的状态
anim.fillMode = kCAFillModeForwards;
// 3. 添加动画对象到图层上
[_myView.layer addAnimation:anim forKey:@"translate"];
// 1. 说明 动画执行对象属性
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = 1;
// 2. 设置 3D 图形运动
CATransform3D form = CATransform3DMakeTranslation(350, 350, 0);
anim.toValue = [NSValue valueWithCATransform3D:form];
// 3. 添加动画对象到图层上
[_myView.layer addAnimation:anim forKey:nil];
简介
属性解析
values:NSArray 对象,里面的元素称为 keyframe「关键帧」path:可以设置一个 CGPathRef\CGMutablePathRef,让层跟着路径移动</span>keyTimes:可以为对应的关键帧指定对应的时间点「范围为 0 ~ 1.0」重要属性:calculationMode「计算模式」
CalculationMode 目前提供如下几种模式:
kCAAnimationLinear「默认值」kCAAnimationDiscrete离散的kCAAnimationPaced使得动画均匀进行keyTimes 设置的或者按关键帧平分时间,此时 keyTimes 和 timingFunctions 无效
kCAAnimationCubic对关键帧为座标点的关键帧进行圆滑曲线相连后插值计算kCAAnimationCubicPaced在 kCAAnimationCubic 的基础上使得动画运行变得均匀keyTimes 和 timingFunctions 无效
示例代码
// 1. 创建帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0;
// 2. 设置图层移动路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
CGPathRelease(path);
// 3. 设置动画的执行节奏
// kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速, 临近结束的时候, 会变慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 代理不需要遵守任何协议「因为这些协议是任何对象都遵守的」
// 可以执行的协议方法:
// - (void)animationDidStart:(CAAnimation *)anim 动画开始时执行
// - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 动画结束时执行
anim.delegate = self;
// 4. 添加动画
[self.redView.layer addAnimation:anim forKey:nil];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(Angle2Radian(-5)), @(Angle2Radian(5)), @(Angle2Radian(-5))];
anim.duration = 0.25;
// 动画的重复执行次数
anim.repeatCount = MAXFLOAT;
// 保持动画执行完毕后的状态
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
// 给 UI 控件的图层添加动画,图层动,UI 控件跟着动「因为控件的显示靠图层」
// shake 是和 动画对象绑定的键值对的 key 值
[self.iconView.layer addAnimation:anim forKey:@"shake"];
// 通过 key 值 shake 删除动画对象
[self.iconView.layer removeAnimationForKey:@"shake"];
简介
属性解析
type:动画过渡类型「NSString 类型」fade // 交叉淡化过渡 kCATransitionFade 「不支持过渡方向」
push // 新视图把旧视图推出去 kCATransitionPush
moveIn // 新视图移到旧视图上面 kCATransitionMoveIn
reveal // 将旧视图移开,显示下面的新视图 kCATransitionReveal
cube // 立方体翻滚效果
oglFlip // 上下左右翻转效果
suckEffect // 收缩效果,如一块布被抽走「不支持过渡方向」
rippleEffect // 滴水效果「不支持过渡方向」
pageCurl // 向上翻页效果
pageUnCurl // 向下翻页效果
cameraIrisHollowOpen // 相机镜头打开效果「不支持过渡方向」
cameraIrisHollowClose // 相机镜头关上效果「不支持过渡方向」
subtype:动画过渡方向kCATransitionFromRight // 从右 → 左
kCATransitionFromLeft // 从左 → 右
kCATransitionFromBottom // 从下 → 上
kCATransitionFromTop // 从下 → 下
startProgress:动画起点「占整体动画的百分比」endProgress:动画终点「占整体动画的百分比」示例代码
// 创建转场动画对象
CATransition *anim = [CATransition animation];
anim.type = @"pageCurl"; // 转场类型
anim.subtype = kCATransitionFromRight; // 转场方向
anim.duration = 0.5;
anim.startProgress = 0.0;
anim.endProgress = 0.5;
[self.view.layer addAnimation:anim forKey:nil];
简介
属性解析
animations:用来保存一组动画对象的 NSArray// 1.创建旋转动画对象
CABasicAnimation *rotate = [CABasicAnimation animation];
rotate.keyPath = @"transform.rotation";
rotate.toValue = @(M_PI);
// 2.创建缩放动画对象
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);
// 3.平移动画
CABasicAnimation *move = [CABasicAnimation animation];
move.keyPath = @"transform.translation";
move.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
// 4.将所有的动画添加到动画组中
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[rotate, scale, move];
group.duration = 2.0;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[self.myvie.layer addAnimation:group forKey:nil];
简介
[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间示例代码
[UIView beginAnimations:nil context:nil];
// 添加修改的属性「产生动画」
self.myview.center = CGPointMake(200, 300);
// 动画执行完毕后, 会自动调用self的animateStop方法
// [UIView setAnimationDelegate:self];
// [UIView setAnimationDidStopSelector:@selector(animateStop)];
[UIView commitAnimations];
// 设置动画代理对象,当动画**开始**或者**结束**时会发消息给代理对象
+ (void)setAnimationDelegate:(id)delegate;
// 当动画即将开始时,执行代理对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationWillStartSelector:(SEL)selector;
// 当动画结束时,执行代理对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector;
+ (void)setAnimationDuration:(NSTimeInterval)duration; // 动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay; // 动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate; // 动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // 动画的节奏控制,具体看下面的”备注”
+ (void)setAnimationRepeatCount:(float)repeatCount; // 动画的重复次数
// 如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
// 设置视图view的过渡效果,transition指定过渡类型,cache设置YES代表使用视图缓存,性能较好
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
// 方法 1
+ (void)animateWithDuration:(NSTimeInterval)duration // 动画的持续时间
delay:(NSTimeInterval)delay // 动画延迟delay秒后开始
options:(UIViewAnimationOptions)options // 动画的节奏控制
animations:(void (^)(void))animations // 将改变视图属性的代码放在这个block中
completion:(void (^)(BOOL finished))completion; // 动画结束后,自动调用这个block
// 方法 2
+ (void)transitionWithView:(UIView *)view // 动画的持续时间
duration:(NSTimeInterval)duration // 需要进行转场动画的视图
options:(UIViewAnimationOptions)options // 转场动画的类型
animations:(void (^)(void))animations // 将改变视图属性的代码放在这个block中
completion:(void (^)(BOOL finished))completion; // 动画结束后,自动调用这个block
// 方法 3 UIView 的转场动画
+ (void)transitionFromView:(UIView *)fromView // 要移除的视图
toView:(UIView *)toView // 要添加的视图
duration:(NSTimeInterval)duration // 动画的持续时间
options:(UIViewAnimationOptions)options // 转场动画的类型
completion:(void (^)(BOOL finished))completion; // 动画结束后,自动调用这个block
// 方法 3 调用完毕后,相当于执行了下面两句代码
// 添加toView到父视图
[fromView.superview addSubview:toView];
// 把fromView从父视图中移除
[fromView.superview removeFromSuperview];
简介
属性解析
animationImages:要显示的图片「一个装着 UIImage 的 NSArray」animationDuration:完整地显示一次 animationImages 中的所有图片所需的时间animationRepeatCount:动画的执行次数「默认为 0,代表无限循环」方法解析
- (void)startAnimating;:开始动画- (void)stopAnimating;:停止动画- (BOOL)isAnimating;:是否正在运行动画简介
initWithActivityIndicatorStyle 初始化方法解析
- (void)startAnimating;:开始动画- (void)stopAnimating;:停止动画- (BOOL)isAnimating;:是否正在运行动画UIActivityIndicatorViewStyle 有 3 个值可供选择:
UIActivityIndicatorViewStyleWhiteLarge,大型白色指示器UIActivityIndicatorViewStyleWhite,标准尺寸白色指示器UIActivityIndicatorViewStyleGray,灰色指示器,用于白色背景简介
Box2d「已停止更新」、Chipmunk
使用步骤
物理仿真器「Dynamic Animator」
让物理仿真元素执行具体的物理仿真行为
物理仿真元素「Dynamic Item」
谁要进行物理仿真?
**物理仿真行为「Dynamic Behavior」 **
执行怎样的物理仿真效果?怎样的动画效果?
代码示例
// 1. 创建仿真器,实际使用中用懒加载的方法「这里的仿真器 必须是不易被销毁的属性」
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 2. 创建物理仿真行为「这里用重力行为来代表」
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.controlView]];
// 3. 添加 物理仿真行为
[self.animator addBehavior:gravity];
制作仿真元素
UIDynamicItem 协议 的对象,都能做物理仿真元素已经是仿真元素的控件
UIView:默认遵守了 UIDynamicItem 协议,因此 任何 UI 控件都能做物理仿真
UICollectionViewLayoutAttributes:默认遵守 UIDynamicItem 协议
特点
UIDynamicBehavior
UIDynamicBehavior 都可以独立进行UIDynamic 提供的物理仿真行为
UIGravityBehavior // 重力行为:给定重力方向、加速度,让物体朝着重力方向掉落
UICollisionBehavior // 碰撞行为:通过添加边界「boundary」让物理碰撞局限在某个空间中,实现碰撞效果
UISnapBehavior // 捕捉行为:让物体迅速冲到某个位置「捕捉位置」捕捉到位置之后会带有一定的震动
UIPushBehavior // 推动行为
UIAttachmentBehavior // 附着行为
UIDynamicItemBehavior // 动力元素行为
重力行为
// 添加到重力行为中的所有物理仿真元素
@property (nonatomic, readonly, copy) NSArray* items;
// 重力方向(是一个二维向量)
@property (readwrite, nonatomic) CGVector gravityDirection;
// 重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)
@property (readwrite, nonatomic) CGFloat angle;
// 位移 = 初速度 * 时间 + (1/2)* 加速度 * 时间²
// 量级(用来控制加速度,1.0代表加速度是1000 points/second²)
@property (readwrite, nonatomic) CGFloat magnitude;
// 初始化
// item参数 :里面存放着物理仿真元素
- (instancetype)initWithItems:(NSArray *)items;
// 添加1个物理仿真元素
- (void)addItem:(id <UIDynamicItem>)item;
// 移除1个物理仿真元素
- (void)removeItem:(id <UIDynamicItem>)item;
碰撞行为
// 是否以参照视图的bounds为边界
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
// 设置参照视图的bounds为边界,并且设置内边距
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;
// 碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞)
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
// 代理对象(可以监听元素的碰撞过程)
@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeAllBoundaries;
// 添加一条看不见的路径「自己绘制」作为边框「或在固定图形中碰撞」
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath;
// 添加一条看不见的线「由两个点确定」作为边框
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
捕捉行为
// 用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)
@property (nonatomic, assign) CGFloat damping;
// 初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;
简介
UIDynamicAnimator 类型的对象常见属性
// 参照视图:通过这个视图来确定 仿真范围
@property (nonatomic, readonly) UIView* referenceView;
// 添加到物理仿真器中的所有物理仿真行为
@property (nonatomic, readonly, copy) NSArray* behaviors;
// 是否正在进行物理仿真
@property (nonatomic, readonly, getter = isRunning) BOOL running;
// 代理对象「能监听物理仿真器的仿真过程,比如开始和结束」
@property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;
常见方法
// UIDynamicAnimator 的初始化
// view参数:是一个参照视图,表示物理仿真的范围
- (instancetype)initWithReferenceView:(UIView *)view;
// 添加1个物理仿真行为
- (void)addBehavior:(UIDynamicBehavior *)behavior;
// 移除1个物理仿真行为
- (void)removeBehavior:(UIDynamicBehavior *)behavior;
// 移除之前添加过的所有物理仿真行为
- (void)removeAllBehaviors;
1、前言 在我们体验一款APP时,炫酷的动画往往能让用户体验大幅度提升。想当年我刚学Android的时候,无意中看到蘑菇街购物车的动画效果,把我给激动得,非要在自己的APP中加入那动画,记得当时用费了好大的劲...不提了,说多了都是泪... 先了解下,目前可以实现动画的方式有: 帧动画(Frame) :将一个完整的动画拆分成一张张单独的图片,然后再将它们连贯起来进行播放 特点:帧动画 由于是一帧一...
Android的基础动画有四种 1.Tween Animation —— 变换动画 2.Layout Animation —— 布局动画 3.Frame Animation —— 帧动画 4.Property Animation —— 属性动画 两种实现方法 1.配置文件(/res/anim) &mda...
show()//从左上角开始显示DOM元素 hide()//从左上角开始隐藏DOM元素 toggle()//反选以上左上角操作 slideUp()//垂直收缩DOM元素 slideDown()//垂直展开DOM元素 slideToggle()//以上垂直操作 PS:但实际上toggle的作用是和slideToggle相同。 实战演练: 运行截图:...
Android的动画可分为三种 View动画 帧动画 属性动画 View动画 View动画有四种效果: 平移:TranslateAnimation 缩放:ScaleAnimation 旋转:RotateAnimation 透明:AlphaAnimation 它们对应的XML文件的标签为: - <translate/> - <scale/> - <rotate/>...
上一篇的动画介绍谈了动画的一些基本使用方法,这一篇谈谈动画的进一步使用。 ObjectAnimator 对属性使用该动画时,要提供setter/getter方法 ObjectAnimator.ofXXX()创建动画对象 调用start()方法开始动画 可以为动画设置时间等 为ObjectAnimator同时设置多个动画属性: ViewPropertyAnimator ViewPropertyAni...
iOS页面切换动画实现方式。 1.使用[color=darkred]UIView animateWithDuration:animations:completion[/color]方法 2.使用UIView beginAnimations:context和UIView commitAnimations方法 3.使用QuartzCore框架中的CATransition...
iOS动画分为基础动画和核心动画 本文发布在http://he8090.cn/2016/07/18/iOS动画/ iOS中关键动画如下 CALayer动画 CABasicAnimation 基础动画 CAKeyFrameAnimation 关键帧动画 CaTransition 过度、转场动画 CAAnimationGroup 组合动画 注:以上动画必须加载到layer上执行 基础动画1 基础动画2...
UIView基础动画实现方式一 UIView位置大小动画 UIView颜色动画 UIView透明度动画 UIView基础动画实现方法二 UIView位置大小动画 UIView颜色动画 UIView透明度动画 Spring动画代码实现...
iOS 动画 1:UIKit的动画 2:Core Animation 动画 UIKit 动画 UIKit 的动画构建比较简单,单用途比较容易受限。UIKit 在性能上和 Core Animation并无都采区别,UIKit 其实就是 core animation 的上层抽象,如同 NSOperation 和 GCD。 Core Animation 动画 Animation 包含了 1.基础抽象类(...
上午 打比赛 T1,2爆0,T310分,T450分(暴力万岁) 中午 和lyw下军棋,可惜输了 宿舍很是安详…… 2条dog下了一个中午的棋,最后2遍都只有一个马了 Ihave one mother(???),and kyx has one. 第1题换根的梗讲了一中午非常欢乐 下午 吃饭前和GJY和Velix疯狂讲笑话非常happy Velix再切T2,tql,%%% ...