iOS之动力行为的重力效果及碰撞效果

1、简介:属于UIKit

(1)什么是动力行为?

模拟真实世界中力学相关的电话和交互系统

(2)可以实现的效果

重力、碰撞、吸附、推动、扑捉效果,并且可以组合

3)iOS9 UIDynamicItemGroup 可以统一给 一组元素 添加动力行为

(4)iOS9 可以定义成 以球形方式 去接触 另一个边境 或元素

类名介绍

(1)UIDynamicAnimator动力效果动画播放者

1.initWithReferenceView:初始化动力效果播放者 并指定参考视图

2.addBehavior:添加动力行为

3.removeBehavior:移除某个动力行为

4.removeAllBehaviors 移除所有动力行为

5.delegate 代理

(2)UIDynamicBehavior 动力效果的动画行为 会影响到动力元素的属性(frame)


《1》UIGravityBehavior 重力效果行为
1.initWithItems:初始化重力效果行为 并指定作用对象
                2.addItem:添加重力效果作用对象
                3.removeItem:移除作用对象
                4.gravityDirection 重力的方向
                CGVector:表示矢量的结构体 值:0-1
                        x:横向重力
                        y:竖向重力
                        受加速度的影响
                5.Angle:更改重力效果的角度  
                6.magnitude:加速度的级别 1.0代表加速度是1000 points /second²
                7.setAngle:magnitude:设置重力方向的角度 和速度

《2》UICollisionBehavior 碰撞行为

《1》元素碰撞的时候 调用
                        1.- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p
                        2.- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2
                    《2》边境碰撞的时候 调用
                        1.- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p
                        2.- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier
(4)addBoundaryWithIdentifier:forPath: 添加路径 为边境
(5)- (nullable UIBezierPath *)boundaryWithIdentifier:(id <NSCopying>)identifier
(6)addBoundaryWithIdentifier:fromPoint:toPoint: 添加一条线为边境
(7)removeBoundaryWithIdentifier
(8)translatesReferenceBoundsIntoBoundary 是否 以参照视图作为边界   
(9)setTranslatesReferenceBoundsIntoBoundaryWithInsets:设置参照视图的内间距

《3》UIPushBehavior

《4》UISnapBehavior

《5》UIAttachmentBehavior 附着效果

《6》UIDynamicItemBehavior

下面是重力效果的案例:
代码如下:
@property (weak, nonatomic) IBOutlet UIImageView *imageView;//故事版中的imageView

#import "ViewController.h"

@interface ViewController ()<UIDynamicAnimatorDelegate>
//动力效果的元素
@property (nonatomic,strong)UIImageView *ballItem;

@property (nonatomic,strong)UIDynamicAnimator *animator;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.imageView addSubview:self.ballItem];
    
}
//MARK:----------动力效果操纵者的代理方法-------

-(void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator{
    NSLog(@"开始");
}
-(void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator{
    NSLog(@"暂停");
    
}
//懒加载
-(UIImageView *)ballItem{
    if (_ballItem) {
        return _ballItem;
    }
    _ballItem = [[UIImageView alloc]initWithFrame:CGRectMake(100, 50, 50, 50)];
    _ballItem.image = [UIImage imageNamed:@"球"];
    return _ballItem;
}
-(UIDynamicAnimator *)animator{
    if (_animator) {
        return _animator;
    }
    _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    _animator.delegate = self;
    return _animator;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.ballItem.center = [[touches anyObject]locationInView:self.view];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.ballItem.center = [[touches anyObject]locationInView:self.view];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"Y:%f",self.ballItem.frame.origin.y);
    
    [self gravity];
}
//MARK:------重力效果-------
-(void)gravity{
//    移除之前效果
    [self.animator removeAllBehaviors];
    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[self.ballItem]];
    /*
     CGVector 表示 方向的结构体
     struct CGVector {
     CGFloat dx; x轴的方向
     CGFloat dy; y轴的方向
     };
     gravityDirection 默认(0.0,1.0)向下 每秒下降 1000个像素点
     */

    gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0);
//    弧度 影响到重力的方向
    gravityBehavior.angle = 30*M_PI/180;
//    magnitude 影响下降速度
    gravityBehavior.magnitude = 100;
    //    把重力效果 添加到 动力效果的操纵者上
    [self.animator addBehavior:gravityBehavior];
    
}
重力效果.gif

下面是碰撞效果:

//MARK:----------检验碰撞的行为---------
-(void)collision{
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[self.ballItem]];
//    设置 检测碰撞的 模式
    collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
//    以参照视图为边境范围
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    
    [self.animator addBehavior:collisionBehavior];
}

注意要在结束之后调用
[self collision];

这样碰撞的效果就实现了:

碰撞效果.gif

另外还可以设置限制:

-(void)collision{
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[self.ballItem]];
//    设置 检测碰撞的 模式
    collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
//    以参照视图为边境范围
//    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 300, 300, 300) cornerRadius:150];
    [collisionBehavior addBoundaryWithIdentifier:@"round" forPath:path];
    
    [self.animator addBehavior:collisionBehavior];
    
}
碰撞限制效果.gif

下面我们设置当球碰撞结束的时候,出现一个图片
首先创建一个图片:

//碰撞到边界 出现的图片
@property (nonatomic,strong)UIImageView *dungView;

-(UIImageView *)dungView{
    if (_dungView) {
        return _dungView;
    }
    UIImage *image = [UIImage imageNamed:@"keng"];
    _dungView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    _dungView.image = image;
    [self.view addSubview:_dungView];
    return _dungView;
}

//MARK:-----------检测碰撞行为的代理方法--------

//检测 元素与边界之间 碰撞

  • (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p{
    NSLog(@"开始 接触 到 边境");

self.dungView.center = CGPointMake(p.x-10, p.y-10);
[self.imageView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

}

  • (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier{
    NSLog(@"结束 接触边镜");

}



![碰撞出现图片效果1.gif](http://upload-images.jianshu.io/upload_images/1401036-db7320f61fb13c70.gif?imageMogr2/auto-orient/strip)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容