iOS开发——总结篇&常用开发总结

时间:2023-03-09 04:33:45
iOS开发——总结篇&常用开发总结

一、通知
1.监听通知

  • - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
  • 当anObject对象发布一条名字叫做aName的通知时,就会调用observer的aSelector方法

2.发布通知

  • // 发布一个通知对象(name、object、userInfo)
  • - (void)postNotification:(NSNotification *)notification;
  • // anObject发布了一个名字叫做aName的通知
  • - (void)postNotificationName:(NSString *)aName object:(id)anObject;
  • // anObject发布了一个名字叫做aName的通知,并且传递了一个额外数据:aUserInfo
  • - (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

二、从storyboard里面加载控制器
1.加载storyboard

  • 1> 方法1:利用storyboard文件的名字
  • + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil;
  • 2> 方法2:控制器的storyboard方法
  • - [UIViewController storyboard]

2.加载控制器

  • // 返回第一个控制器(初始化控制器,箭头所指的控制器)
  • - (id)instantiateInitialViewController;
  • // 返回唯一标识是identifier的控制器
  • - (id)instantiateViewControllerWithIdentifier:(NSString *)identifier;

三、block的定义格式
1.block变量的定义

  • void (^myblock)(int, int);
  • myblock = ^(int a, int b) {
  • };

int age = 10;

2.@property的定义

  • @property (nonatomic, copy) void (^myblock)(int, int);
  • @property (nonatomic, assign) int age;

3.当做方法的参数

  • - (void)setMyBlock:(void (^)(int, int))myblock;
  • - (void)setAge:(int)age;

四、CGRect常见函数

  • 1.CGRectGetMinX(CGRect rect) 等函数
  • 2.CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)

五、UIImage的裁剪

  • CGImageCreateWithImageInRect(<#CGImageRef image#>, <#CGRect rect#>)

六、消除锯齿

  • - (void)clearAlias
  • {
  • self.layer.borderWidth = 2;
  • self.layer.borderColor = [UIColor clearColor].CGColor;
  • // 就会把图层当做是一个bitmap来渲染
  • self.layer.shouldRasterize = YES;
  • for (UIView *child in self.subviews) {
  • [child clearAlias];
  • }
  • }

七、计时器
1.NSTimer

  • 1> 人为控制刷新频率
  • 2> 对刷新速度要求不高,适合慢刷新
  • 3> 创建timer
  • // 返回一个新的timer,但是不会开始计时,需要调用fire方法
  • + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
  • // 返回一个新的timer,会马上开始计时
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
  • 4> 停止计时器(只要计时器停止了,以后不能再次开始计时)
  • - (void)invalidate;

2.CADisplayLink

  • 1> 包含QuartzCore框架
  • 2> 固定刷新频率(1秒钟刷新60次)
  • 3> 对刷新速度要求高,适合快刷新
  • 4> 创建displaylink
  • // 返回一个CADisplayLink计时器对象,1秒内会调用60次target的sel方法,并且将CADisplayLink当做参数传入
  • + (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel;
  • 5> 开始计时
  • - (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode;
  • 6> 停止计时
  • - (void)invalidate;
  • 7> 刷帧间隔
  • @property(readonly, nonatomic) CFTimeInterval duration;
  • 8> 控制暂停或者继续
  • @property(getter=isPaused, nonatomic) BOOL paused;

八、动画
1.UIView封装的动画

  • 1> 首尾式
  • [UIView beginAnimations:nil context:nil];
  • // ... 需要执行怎样的动画
  • [UIView commitAnimations];

2> block

  • [UIView animateWithDuration:0.5 animations:^{
  • // 需要执行的动画
  • } completion:^(BOOL finished) {
  • // 动画完成
  • }];

3> 转场动画(过渡动画)

  • // 让某个view执行转场动画
  • [UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];

2.CALayer的动画

  • // CABasicAnimation和CAKeyframeAnimation的keyPath可以是哪些值?
  • // 在xcode文档搜索:CALayer Animatable Properties
  • // transform的具体属性:搜索catransform3d key path

1> CABasicAnimation

  • * fromValue  初始值
  • * toValue  最终值  (从初始化变化到最后某一个值)
  • * byValue  步进值  (在初始值的基础上,增加多少值)

2> CAKeyframeAnimation

  • * values

3> CATransition(转场动画)

  • CATransition *anim = [CATransition animation];
  • anim.type = @"cube";
  • anim.subtype = kCATransitionFromBottom;
  • [view.layer addAnimation:anim forKey:nil];

4> CAAnimationGroup

  • * 动画,可以同时执行多个动画

3.如何选择动画

  • 1> 如果需要重复执行多次动画,最好选择CALayer动画
  • 2> 如果动画执行完毕后,就要用到前面的一些东西,最好选择UIView的block动画
  • 3> 如果需要同时执行多个动画,最好选择CAAnimationGroup
  • 4> UIView动画和CALayer动画中最灵活的是CALayer的动画

4.自定义一些动画

  • 用CADisplayLink,在刷帧方法完成需要执行的动画