·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 杂记---不定期更新

杂记---不定期更新

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

注:再本文中您将可能看到排版混乱,中英文混用,表达不清等问题,敬请见谅。中英文混用是因为我想保留英文的意思,避免错翻译(其实是我英文水平不够根本就不知道什么意思)。

------------------------------------------------------------- 碎 片 知 识 ----------------------------------------------------------------------------

1.data source也是一种特殊的delegate,data source负责传输数据给View例如data、at、count等,而delegate负责处理View的did、should、will等问题,View把需要的数据信息或者事件信息传输给dalegate,dalegate则负责回复这些信息。

--------------------------------------------------------------- Block -------------------------------------------------------------------------------

1.Block当中不能修改局部变量的值,在block中,局部变量都是只读的,比如在block外部有一个BOOL value = YES;,如果在block内部修改value的值,这就是非法的,编译器会报错。当然,有一种办法可以进行读写,就是在变量前面加上两个下划线和block,如:__block BOOL value = YES;,这样就可以在block当中进行该变量的读写了。

int anInteger = 42;


void (^testBlock)(void) = ^{

    NSLog(@"Integer is: %i", anInteger);

};


anInteger = 84;


testBlock();

// 输出结果为:Integer is: 42

// It also means that the block cannot change the value of the original variable
// 在block defined的那一刻anInteger的值就已经被捕获了
辅助理解

原理是如果声名了__block,编译器就会生成一段代码将value从栈(stack)移到堆中(heap),同时编译器会有一个强指针指向block中的每一个变量,确保它不会在block执行完毕前就离开堆,当执行完block的时候,编译器将信息拷贝回堆当中,然后再移回栈当中,同时强指针会在block goes out of scope*1之前一直存在。

2.如果block不需要参数,参数的括号可以直接省去,如果返回值再return的时候可以直接看出来,比如return [MyClass isKindOfClass:...];,这种类型的编译器会自动完成返回值,所以block的返回值类型也可以省去。

--------------------------------------------------------------- 动 画 --------------------------------------------------------------------------------

1.使用animateWithDuration来执行动画的时候如果动画被中途中断了,比如说将alpha从1.0设置为0,如果执行中途有代码或者另一个动画设置了alpha,则Completion的block的参数finished则为NO,如果没有人中断完成了动画,则finished为YES。

2.UIViewAnimationOptions:

  1. BeginFromCurrentState:如果开启了该选项,如果执行第一个动画将alpha变为0,在执行到变为0.5的时候(虽然代码是立即变为0,但是此处为看到的动画),如果动画二将alpha变为1,并且开启了这个选项,则alpha会从0.5开始变为1。如果没有开启则会从0开始变为1。
  2. CurveEaseInEaSEOut和CurveEaseIn:如果开启了该选项,则会在执行动画的时候使用线性运动(开始慢,中间加速,结束慢)。

3.使用transitionWithView:(UIView *)view              方法来修改整个视图的状态。

        duration:(NSTimeIntevel)duration

        options:(UIViewAnimationOptions)options

        animations:(void (^)(void))animations

        completion:(void (^)(BOOL finished))completion;

4.transitionWithView的options:

  1. UIViewAnimationOptionsTransitionFlipFrom{Left, Right, Top, Bottom},代表翻转整个View(选项是从哪个地方翻转)。
  2. UIViewAnimationOptionsTransitionCrossDissolve,代表交叉溶解。
  3. UIViewAnimationOptionsTransitionCurl{Up, Down},代表从哪个方向卷起。

5.transitionFromView:(UIView *)fromView            使用toView取代fromView。

        toView:(UIView *)toView

        duration:(NSTimeInteval)duration

        options:(UIViewAnimationOptions)options

        completion:(void (^)(BOOL finished))completion;

--------------------------------------------------------- DynamicAnimator -------------------------------------------------------------------------

1.创建动力动画的步骤:

  1. 创建一个UIDynamicAnimator对象。
  2. 给它加入UIDynamicBehaviors(gravity, collisions, etc)。
  3. 给UIDynamicBehaviors加入UiDynamicItems。

2.创建UIDynamicAnimator对象的时候直接使用alloc/init是可以的,但是如果作用于一个UIView对象则需要使用alloc/initWithReferenceView:方法来指定顶级视图。UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:aView];

3.创建UIDynamicBehavior的时候直接使用alloc/init就可以了,然后将其加入UIDynamicAnimator中。UIGravityBehavior *gravity = [[UIGravity Behavior alloc] init];[animator addBehavior:gravity];

4.添加Item:id <UIDynamicItem> item1 = ...;[gravity addItem:item1];基本上90%的item都是UIView。

5.UIDynamicItem协议属性方法:

  1. @PRoperty (readonly) CGRect bounds;
  2. @property (readwirte) CGPoint center;
  3. @property (readwrite) CGAffineTransform transform;

6.如果要在animator执行动画的时候修改item的属性,必须要使用- (void)updateItemUsingCurrentState:(id <UIDynamicItem>)item;方法,不然animator会忽略所有其他的修改。

7.UIGravityBehavior(重力行为--控制Items的角度以及重力系数)属性方法:

  1. @property CGFloat angle;
  2. @property CGFloat magnityde; // 1.0 is 1000 points/second/square

8.UICollisionBehavior(碰撞行为--行为中的Items碰撞在一起的时候会撞开,和现实中碰撞一样,同时可以设置边界,置顶弹力大小或者密度)属性方法:

  1. @property UICollisionBehaviorMode collisionMode; // Items, boundaries, Everything(default)
  2. - (void)addBoundaryWithIdentifier:(NSString *)identifier for Path:(UIBezierPath *)path; // 增加边界
  3. @property BOOL translatesReferenceBoundsIntoBoundary; // 将其设为YES,则会将animator的referenceView的边界作为边界

9.UIAttachmentBehavior(吸附行为--默认情况下吸附两者之间的间距想一个铁棒,通过对其阻尼大小(damping)和震荡频率(oscillation)进行设置可以使其变得想弹簧一样)属性方法:

  1. - (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)anchor;
  2. - (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
  3. - (instancetype)initWithItem:(id <UIDynamicItem>)item offsetFromCenter:(CGPoint)offset...
  4. @property (readwrite) CGFloat length; // 两个吸附物之间的间距
  5. 同时也可是设置阻尼以及震荡频率
  6. @property (readwrite) CGPoint anchorPoint; // 能随时重新设置

10.UISnapBehavior(速甩行为我觉得这个翻译实在是不好)属性方法:

  1. - (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point; // 可以想象成在移动到新位置的时候item的四个角都有弹簧,它会晃一下来给予用户反馈(哦!我到达了!)

11.UIPushBehavior(推动行为)属性方法:

  1. @property UIPushBehaviorMode mode;
  2. @property CGVector pushDirection;
  3. @property CGFloat magnitude/angle; // 将magnitude设置为1.0将会使用100 points/second/squared 移动100x100的view

12.UIDynamicItemBehavior(动力项行为--如果想要设置了例如摩擦力、弹力、密度等,不管进行什么行为,它的这些属性应该是一样的,他们都是在UIDynamicItemBehavior类中设置)属性方法:

  1. @property BOOL allowsRotation; // 是否可以旋转
  2. @property BOOL friction; // 摩擦力
  3. @property BOOL elasticity; // 弹力
  4. @property CGFloat density; // 密度
  5. - (CGPoint)linearVelocityForItem:(id <UIDynamicItem>)item; // 获得动力项不同方向的速度
  6. - (CGPoint)angularVelocityForItem:(id <UIDynamicItem>)item; // 获得动力项不同角度的速度

13.UIDynamicBehavior是所有行为的子类。可以使用- (void)addChildBehavior:(UIDynamicBehavior *)behavior;方法来添加子行为。

14.所有的behaviors都拥有一个属性@property UIDynamicAnimator *dynamicAnimator;它们记录的是所属UIDynamicAnimator的信息。同时拥有一个方法- (void)willMoveToAnimator:(UIDynamicAnimator *)animator;,该方法会在behavior加入到animator或者移出的时候被调用,加入的时候参数是目标animator,移除的时候参数会被设置成nil。

15.UIDynamicBehavior的action property:@property (copy) void (^action)(void) 想要使用这个属性,只要使用任意behavior加上.action并且赋值一个block,那么在每次执行behavior的时候都会进行调用该block。

 --------------------------------------------------------------- 多 线 程 ----------------------------------------------------------------------------

1.线程可以理解为queue,queue中都是blocks。

2.MainQueue存储的是需要改变屏幕显示或者同步(让一切改变和UI同步)等操作的block。

3.在另一个线程执行block:

  1. dispatch_queue_t queue = ...; // typedef 类型
  2. dispatch_async(queue, ^{  }); // 将目标队列以及要执行的block作为参数传入

4.获得主要线程:

  1. dispatch_queue_t queue = dispatch_get_main_queue();
  2. NSOperationQueue *mainQ = [NSOperationQueue mainQueue]; // for object-oriented APIs

5.dispatch_queue_t otherQ = dispatch_queue_create("name", NULL); // 第一个是queue的名字,第二个则是指定queue是serial queue(串行队列)或者concurrent queue(并发队列),NULL代表是一个serial queue

6.将block放回主线程:

 - (void)performSelectorOnMainThread:(SEL)aMethod

              withObject:(id)obj    // selector方法的参数,可以为nil

            waitUntilDone:(BOOL)waitUntilDone; // is whether you're gonna wait until this thing gets pulled off the main queue and run on the main queue and then finishes before this thread, that's calling this, goes or not.                                   (英文部分不是很理解),通常设置为NO,不需要等待,会直接再主队列上调用这个方法, 轮到它执行的时候就执行

 dispatch_async(dispatch_get_main_queue(), ^{ /* call aMethod */ })  

7.使用多线程的例子:

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@"http://..."]];

  NSURLConfiguration *configuration = ...;

  NSURLsession *session = ...;

  NSURLSessionDownloadTask *task;

  task = [session downloadTaskWithRequest:request

                completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {

  /* want to do UI things here can I? */ /* must be main queue */

  }];

  [task resume];

8.创建Session的方法:

  NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration

                                delegate:nil

                             delegateQueue:[NSOperationQueue mainQueue]];

  NSURLSessionDownloadTask *task;

  task = [session downloadTaskWithRequest:request

              completionHandler:^(NSURL *localfile, NSURLResponse *response, NSErr or *error) {

    / * yes, can do UI things directly because this is called on the main queue */

  }];

  [task resume];

9.没有代理以及代理线程的创建session方法

  NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; // no delegateQueue

  NSURLSessionDownloadTask *task;

  task = [session downloadTaskWithRequest:request

               completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {

    disapatch_async(dispatch_get_main_queue(), ^( /* do UI things */ ));

    or [self performSelectorOnMainThread:@selector(doUIthings) withObject:nil waitUntilDone:NO];

  }];

  [task resume];

10.ScrollView获得当前显示区域在整个ScollView中的坐标

  CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:subview];

11.- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated; // 对rect进行尽可能的方法或者缩小(显示在屏幕上)

 ------------------------------------------------------------- 表格视图和iPad --------------------------------------------------------------------------

 


突然感觉学的好像有点快了,开始回头看以前知识的详细解释。

*1:英语不是很好,我的理解是block也是有作用域的,比如[UIView animateWithDuration:NSTimeInterval animations:^(void)animations]这个方法,再执行完该方法的时候可能block就已经失效了,block的作用与就是这一条语句,当然这是我的猜测,如有了解的请务必纠正我。

 

火狐的restart命令会让博客园自动保存内容全部挂掉?明明自动保存了为什么重启后读取保存的几个小时前的数据?写了几个小时就这么没了,瞬间不想再写博客了。

还是屈服了,从第4点开始可能记得有点乱,因为之前记了很多结果被该死的自动保存干掉了...

 

有点乱,分个类先