IOS7笔记-8协议block动画

Posted Faint@LastStep

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS7笔记-8协议block动画相关的知识,希望对你有一定的参考价值。

1、动画设置方法

  1)定义动画Animator

1 @property (strong, nonatomic) UIDynamicAnimator *animator;
2 
3 -(UIDynamicAnimator *)animator
4 {
5     if (!_animator) {
6         _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
7     }
8     return _animator;
9 }

  2)定义行为Behavior,并将行为添加到动画中

 1 @property (strong, nonatomic) UIGravityBehavior *gravity;//重力
 2 @property (strong, nonatomic) UICollisionBehavior *collider;//碰撞
 3 
 4 -(UIGravityBehavior *)gravity
 5 {
 6     if (!_gravity) {
 7         _gravity = [[UIGravityBehavior alloc] init];
 8         _gravity.magnitude = 0.9;
 9         [self.animator addBehavior:_gravity];
10     }
11     return _gravity;
12 }
13 
14 -(UICollisionBehavior *)collider
15 {
16     if (!_collider) {
17         _collider = [[UICollisionBehavior alloc] init];
18         _collider.translatesReferenceBoundsIntoBoundary = YES;
19         [self.animator addBehavior:_collider];
20     }
21     return _collider;
22 }

  3)实现动画视图,并添加到相关的行为中

 1 -(void)drop
 2 {
 3     CGRect frame;
 4     frame.origin = CGPointZero;
 5     frame.size = DROP_SIZE;
 6     int x = (arc4random()%(int)self.gameView.bounds.size.width) / DROP_SIZE.width;
 7     frame.origin.x = x * DROP_SIZE.width;
 8     
 9     UIView *dropView = [[UIView alloc] initWithFrame:frame];
10     dropView.backgroundColor = [self randomColor];
11     [self.gameView addSubview:dropView];
12     
13     [self.gravity addItem:dropView];
14     [self.collider addItem:dropView];
15 }
16 
17 -(UIColor *)randomColor
18 {
19     switch (arc4random()%5) {
20         case 0: return [UIColor greenColor];
21         case 1: return [UIColor blueColor];
22         case 2: return [UIColor orangeColor];
23         case 3: return [UIColor redColor];
24         case 4: return [UIColor purpleColor];
25     }
26     return [UIColor blackColor];
27 }

  4)在其他地方,如手势事件中调用以上Drop方法

1 - (IBAction)tap:(UITapGestureRecognizer *)sender {
2     [self drop];
3 }

 2、实现自定义动画行为类方法

  1)先创建一个UIDynamicBehavior子类DropitBehavior,并添加两个公共API方法AddItem和RemoveItem

1 #import <UIKit/UIKit.h>
2 
3 @interface DropitBehavior : UIDynamicBehavior
4 
5 -(void)addItem:(id<UIDynamicItem>)item;
6 -(void)removeItem:(id<UIDynamicItem>)item;
7 
8 @end

  2)定义两个行为或自定义行为

 1 @interface DropitBehavior()
 2 @property (strong, nonatomic) UIGravityBehavior *gravity;
 3 @property (strong, nonatomic) UICollisionBehavior *collider;
 4 @end
 5 
 6 -(UIGravityBehavior *)gravity
 7 {
 8     if (!_gravity) {
 9         _gravity = [[UIGravityBehavior alloc] init];
10         _gravity.magnitude = 0.9;
11     }
12     return _gravity;
13 }
14 
15 -(UICollisionBehavior *)collider
16 {
17     if (!_collider) {
18         _collider = [[UICollisionBehavior alloc] init];
19         _collider.translatesReferenceBoundsIntoBoundary = YES;
20     }
21     return _collider;
22 }

  3)实现两个公共API方法,并实现初始化init将自定义行为做为子行为添加至当前类

 1 -(void)addItem:(id<UIDynamicItem>)item
 2 {
 3     [self.gravity addItem:item];
 4     [self.collider addItem:item];
 5 }
 6 
 7 -(void)removeItem:(id<UIDynamicItem>)item
 8 {
 9     [self.gravity removeItem:item];
10     [self.collider removeItem:item];
11 }
12 
13 -(instancetype)init
14 {
15     self = [super init];
16     [self addChildBehavior:self.gravity];
17     [self addChildBehavior:self.collider];
18     return self;
19 }

  4)在控制器中调用

 1 #import "DropitBehavior.h"//导入自定义类
 2 
 3 @property (strong, nonatomic) UIDynamicAnimator *animator;
 4 @property (strong, nonatomic) DropitBehavior *dropitBehavior;//声明
 5 
 6 -(UIDynamicAnimator *)animator
 7 {
 8     if (!_animator) {
 9         _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
10     }
11     return _animator;
12 }
13 
14 -(DropitBehavior *)dropitBehavior
15 {
16     if (!_dropitBehavior) {
17         _dropitBehavior = [[DropitBehavior alloc] init];
18         [self.animator addBehavior:_dropitBehavior];
19     }
20     return _dropitBehavior;
21 }
22 
23 - (IBAction)tap:(UITapGestureRecognizer *)sender {
24     [self drop];
25 }
26 
27 -(void)drop
28 {
29     CGRect frame;
30     frame.origin = CGPointZero;
31     frame.size = DROP_SIZE;
32     int x = (arc4random()%(int)self.gameView.bounds.size.width) / DROP_SIZE.width;
33     frame.origin.x = x * DROP_SIZE.width;
34     
35     UIView *dropView = [[UIView alloc] initWithFrame:frame];
36     dropView.backgroundColor = [self randomColor];
37     [self.gameView addSubview:dropView];
38     
39     [self.dropitBehavior addItem:dropView];
40 }
41 
42 -(UIColor *)randomColor
43 {
44     switch (arc4random()%5) {
45         case 0: return [UIColor greenColor];
46         case 1: return [UIColor blueColor];
47         case 2: return [UIColor orangeColor];
48         case 3: return [UIColor redColor];
49         case 4: return [UIColor purpleColor];
50     }
51     return [UIColor blackColor];
52 }

 3、动画委托中实现Did(Will)相关方法

  1)声明中定义委托

 1 @interface DropitViewController ()<UIDynamicAnimatorDelegate>
 2 @property (weak, nonatomic) IBOutlet UIView *gameView;
 3 @property (strong, nonatomic) UIDynamicAnimator *animator;
 4 @property (strong, nonatomic) UIGravityBehavior *gravity;
 5 @property (strong, nonatomic) UICollisionBehavior *collider;
 6 @property (strong, nonatomic) DropitBehavior *dropitBehavior;
 7 @end
 8 
 9 -(UIDynamicAnimator *)animator
10 {
11     if (!_animator) {
12         _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.gameView];
13         _animator.delegate = self;
14     }
15     return _animator;
16 }

  2)实现相关委托方法

 1 //这是一个动画暂停发生后事件
 2 -(void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator
 3 {
 4     [self removeCompletedRows];
 5 }
 6 //遍历GameView视图将满足一行的全部Drop子视图搜索出来,并从中动画消除
 7 - (BOOL)removeCompletedRows
 8 {
 9     NSMutableArray *dropsToRemove = [[NSMutableArray alloc] init];
10     
11     for (CGFloat y = self.gameView.bounds.size.height-DROP_SIZE.height/2; y > 0; y -= DROP_SIZE.height)
12     {
13         BOOL rowIsComplete = YES;
14         NSMutableArray *dropsFound = [[NSMutableArray alloc] init];
15         for (CGFloat x = DROP_SIZE.width/2; x <= self.gameView.bounds.size.width-DROP_SIZE.width/2; x += DROP_SIZE.width)
16         {
17             UIView *hitView = [self.gameView hitTest:CGPointMake(x, y) withEvent:NULL];
18             if ([hitView superview] == self.gameView) {
19                 [dropsFound addObject:hitView];
20             } else {
21                 rowIsComplete = NO;
22                 break;
23             }
24         }
25         if (![dropsFound count]) break;
26         if (rowIsComplete) [dropsToRemove addObjectsFromArray:dropsFound];
27     }
28     
29     if ([dropsToRemove count]) {
30         for (UIView *drop in dropsToRemove) {
31             [self.dropitBehavior removeItem:drop];
32         }
33         [self animateRemovingDrops:dropsToRemove];
34     }
35     
36     return NO;
37 }
38 //炸掉一行的动画效果
39 -(void)animateRemovingDrops:(NSArray *)dropsToRemove
40 {
41     [UIView animateWithDuration:1.0
42          animations:^{
43              for (UIView *drop in dropsToRemove) {
44                  int x = (arc4random()%(int)(self.gameView.bounds.size.width*5)) - (int)(self.gameView.bounds.size.width*2);
45                  int y = self.gameView.bounds.size.height;
46                  drop.center = CGPointMake(x, -y);
47              }
48          }
49          completion:^(BOOL finished){
50              [dropsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
51          }];
52 }

 

以上是关于IOS7笔记-8协议block动画的主要内容,如果未能解决你的问题,请参考以下文章

《Motion Design for iOS》(十八)

Objective-C基础笔记Block

iOS7 push/pop转场动画

适用于 iOS 8 但不适用于 iOS 7 的简单动画

iOS7中的NSLayoutConstraint动画持续时间

在jquery中切换动画