UIView动画初探

UIView常用属性

@interface UIView(UIViewGeometry)
@property(nonatomic,getter=isExclusiveTouch) BOOL      exclusiveTouch __TVOS_PROHIBITED;// default is NO
@property(nonatomic,readonly,strong)                 CALayer  *layer; 
@end

ExclusiveTouch

A Boolean value that indicates whether the receiver handles touch events exclusively.Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO. – Apple

作用: 接收点击事件的排他性。设置为YES时,则当本控件接收到点击事件,则同一窗口(window)中其他控件无法接收到触摸事件。 因为本控件已经阻塞了触摸事件的传递。
应用: 避免一个触摸事件触发多个按钮相应。
Demo:

-(void)setExclusiveTouchForButtons:(UIView *)myView
{
    for (UIView * v in [myView subviews]) {
        if([v isKindOfClass:[UIButton class]])
            [((UIButton *)v) setExclusiveTouch:YES];
        else if ([v isKindOfClass:[UIView class]]){
            [self setExclusiveTouchForButtons:v];
        }
    }
}

或者在AppDelegate

[[UIButton appearance] setExclusiveTouch:YES];// **这个方法仅支持iOS 8.0+系统**

文/MindTheGap(简书作者)
原文链接:http://www.jianshu.com/p/8c505df3b16d

layer属性

返回主Layer实例

DrawRect

是否应该实现DrawRect

最快的绘制就是你不要做任何绘制。大多数时间,你可以不要合成你在其他视图(图层)上定制的视图(图层),这正是我们推荐的,因为 UIKit 的视图类是非常优化的 (就是让我们不要闲着没事做,自己去合并视图或图层) 。 – from ObjC中国-绘制像素到屏幕上

drawRect会被调用的情况

1) 调用sizeToFit后被调用(ps: 调用sizeToFit计算出size,然后系统自动调用drawRect方法);[提倡]
2) 通过设置contentMode属性值为UIViewContentModeRedraw,则在每次设置或更改frame的时候自动调用drawRect:。[不提倡]
3) 调用setNeedsDisplay/setNeedsDisplayInRect: 会触发drawRect: ,前提是rect不为0;[不提倡]
drawRect是在loadView,viewDidLoad两方法后调用。该方法无法手动调用,只有通过调用其他方法触发.
ps : 初始化时未设置rect,则不自动调用drawRect。

UIView动画属性

// ? frame与bounds设置动画属性时有什么区别
@property(nonatomic) CGRect frame;// 大小变化,改变位置和大小
@property(nonatomic) CGRect bounds;// 拉伸变化,改变位置和大小
@property(nonatomic) CGPoint center;// 位置修改
@property(nonatomic) CGAffineTransform transform;// 旋转
@property(nonatomic) CGFloat alpha;// 透明度修改,会创建淡入淡出的效果
@property(nonatomic, copy) UIColor *backgroundColor;// 颜色转变

@property(nonatomic) CGRect contentStretch;// 拉伸 iOS6.0 deprecated
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;

动画过程

// 1.开始动画
[UIView beginAnimations:(nullableNSString*)context:(nullablevoid*)];
// 2.动画参数设置
//动画持续时间
[UIView setAnimationDuration:(NSTimeInterval)];
//动画的代理对象
[UIView setAnimationDelegate:(nullableid)];
//设置动画将开始时代理对象执行的SEL
[UIView setAnimationWillStartSelector:(nullableSEL)];
//设置动画结束时代理对象执行的SEL
[UIView setAnimationDidStopSelector:(nullableSEL)];
//设置动画延迟执行的时间
[UIView setAnimationDelay:(NSTimeInterval)];
//设置动画的重复次数
[UIView setAnimationRepeatCount:(float)];
//设置动画的曲线
[UIView setAnimationCurve:(UIViewAnimationCurve)];
UIViewAnimationCurve的枚举值如下:
UIViewAnimationCurveEaseInOut,//慢进慢出(默认值)
UIViewAnimationCurveEaseIn,//慢进
UIViewAnimationCurveEaseOut,//慢出
UIViewAnimationCurveLinear//匀速
//设置是否从当前状态开始播放动画
[UIView setAnimationBeginsFromCurrentState:YES];
// 3.结束动画
[UIView commitAnimations];

移动demo

// from 
-(void)changeFrame{
[UIView beginAnimations:@"FrameAni"context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(startAni:)];
[UIViews etAnimationDidStopSelector:@selector(stopAni:)];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
self.cartCenter.frame=self.centerShow.frame;
[UIView commitAnimations];
}
-(void)startAni:(NSString*)aniID{
NSLog(@"%@start",aniID);
}
-(void)stopAni:(NSString*)aniID{
NSLog(@"%@stop",aniID);
}

转场动画

-(void)flipAni{
[UIView beginAnimations:@"FlipAni"context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(startAni:)];
[UIView setAnimationDidStopSelector:@selector(stopAni:)];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFrom LeftforView:self.centerShowcache:YES];
self.centerShow.image=[UIImage imageNamed:@"service"];
[UIView commitAnimations];
}

Block动画

// 1.简单动画block
[UIViewanimateWithDuration:(NSTimeInterval)//动画持续时间
animations:^{
//执行的动画
}];

// 2. 带动画完成回调
[UIViewanimateWithDuration:(NSTimeInterval)//动画持续时间
animations:^{
//执行的动画
}completion:^(BOOLfinished){
//动画执行完毕后的操作
}];

// 3. 可设置动画延迟时间与过度效果
[UIViewanimateWithDuration:(NSTimeInterval)//动画持续时间
delay:(NSTimeInterval)//动画延迟执行的时间
options:(UIViewAnimationOptions)//动画的过渡效果
animations:^{
//执行的动画
}completion:^(BOOLfinished){
//动画执行完毕后的操作
}];

// 4. Spring动画
[UIView animateWithDuration:(NSTimeInterval)//动画持续时间
delay:(NSTimeInterval)//动画延迟执行的时间
usingSpringWithDamping:(CGFloat)//震动效果,范围0~1,数值越小震动效果越明显
initialSpringVelocity:(CGFloat)//初始速度,数值越大初始速度越快
options:(UIViewAnimationOptions)//动画的过渡效果
animations:^{
//执行的动画
}
completion:^(BOOLfinished){
//动画执行完毕后的操作
}];

// 5.关键帧动画
[UIView animateKeyframesWithDuration:(NSTimeInterval)//动画持续时间
delay:(NSTimeInterval)//动画延迟执行的时间
options:(UIViewKeyframeAnimationOptions)//动画的过渡效果
animations:^{
//执行的关键帧动画
}
completion:^(BOOLfinished){
//动画执行完毕后的操作
}];

// 6. 转场动画
[UIView transitionFromView:(nonnullUIView*)
toView:(nonnullUIView*)
duration:(NSTimeInterval)
options:(UIViewAnimationOptions)
completion:^(BOOLfinished){
//动画执行完毕后的操作
}];

其中UIViewAnimationOptions的枚举值为:

UIViewAnimationOptionLayoutSubviews//进行动画时布局子控件
UIViewAnimationOptionAllowUserInteraction//进行动画时允许用户交互
UIViewAnimationOptionBeginFromCurrentState//从当前状态开始动画
UIViewAnimationOptionRepeat//无限重复执行动画
UIViewAnimationOptionAutoreverse//执行动画回路
UIViewAnimationOptionOverrideInheritedDuration//忽略嵌套动画的执行时间设置
UIViewAnimationOptionOverrideInheritedCurve//忽略嵌套动画的曲线设置
UIViewAnimationOptionAllowAnimatedContent//转场:进行动画时重绘视图
UIViewAnimationOptionShowHideTransitionViews//转场:移除(添加和移除图层的)动画效果
UIViewAnimationOptionOverrideInheritedOptions//不继承父动画设置
UIViewAnimationOptionCurveEaseInOut//时间曲线,慢进慢出(默认值)
UIViewAnimationOptionCurveEaseIn//时间曲线,慢进
UIViewAnimationOptionCurveEaseOut//时间曲线,慢出
UIViewAnimationOptionCurveLinear//时间曲线,匀速
UIViewAnimationOptionTransitionNone//转场,不使用动画
UIViewAnimationOptionTransitionFlipFromLeft//转场,从左向右旋转翻页
UIViewAnimationOptionTransitionFlipFromRight//转场,从右向左旋转翻页
UIViewAnimationOptionTransitionCurlUp//转场,下往上卷曲翻页
UIViewAnimationOptionTransitionCurlDown//转场,从上往下卷曲翻页
UIViewAnimationOptionTransitionCrossDissolve//转场,交叉消失和出现
UIViewAnimationOptionTransitionFlipFromTop//转场,从上向下旋转翻页
UIViewAnimationOptionTransitionFlipFromBottom//转场,从下向上旋转翻页

参考资料

Animations-Apple
UIView-Apple
iOS动画篇:UIView动画
iOS UIView Animation - First Animation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值