iOS 動力效果

相關概念:

 UIPushBehavior :推動效果

 

 typedef NS_ENUM(NSInteger, UIPushBehaviorMode) {

 UIPushBehaviorModeContinuous, 持續的力

 UIPushBehaviorModeInstantaneous 瞬間的力

 } NS_ENUM_AVAILABLE_IOS(7_0);

 @property (nonatomic, readonly) UIPushBehaviorMode mode; 推動效果的樣式

 @property (nonatomic, readwrite) BOOL active; 是否激活

 @property (readwrite, nonatomic) CGFloat angle; 推動角度

 // A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point view whose density value is 1.0, results in view acceleration of 100 points per s^2

 @property (readwrite, nonatomic) CGFloat magnitude; 推動力量

 @property (readwrite, nonatomic) CGVector pushDirection; 推動的方向

 

 

 ------------------------------

 UISnapBehavior:迅速移動效果

 // The point argument is expressed in the reference coordinate system

 - (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;

 迅速移動效果 只能一次 添加到一個元素上  snapToPoint 讓他移動到哪一個點

 

 @property (nonatomic, assign) CGFloat damping; // damping value from 0.0 to 1.0. 0.0 is the least oscillation. damping 的範圍是(0.0-1.0


 ------------------------------

 typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) {

 UIAttachmentBehaviorTypeItems, 吸附一個元素

 UIAttachmentBehaviorTypeAnchor 吸附一個點

 } NS_ENUM_AVAILABLE_IOS(7_0);

 設置吸附效果的樣式

 @property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;

 UIAttachmentBehavior:附着效果

 吸附着一個視圖 或者一個點  (也可以多個連接)

 

 附着效果 一個視圖與一個錨點或者另一個視圖相連接的情況

 附着行爲描述的是兩點之間的連接情況,可以模擬剛性或者彈性連接

 在多個物體間設定多個UIAttachmentBehavior,可以模擬多物體連接

 

 typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) {

 UIAttachmentBehaviorTypeItems, 吸附一個元素

 UIAttachmentBehaviorTypeAnchor 吸附一個點

 } NS_ENUM_AVAILABLE_IOS(7_0);

 設置吸附效果的樣式

 @property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;

 

 @property (readwrite, nonatomic) CGPoint anchorPoint;錨點

 

 @property (readwrite, nonatomic) CGFloat length;距離 與錨點的距離

 @property (readwrite, nonatomic) CGFloat damping; // 1: critical damping  跳躍度

 @property (readwrite, nonatomic) CGFloat frequency; // in Hertz   幅度


具體代碼:

#import "ViewController.h"


@interface ViewController ()

{

   

    UIDynamicAnimator *dynamicAnimator;

    UIView *view1;

    UIView *view2;


}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

   

    dynamicAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];

    

    view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

    view1.backgroundColor = [UIColor colorWithRed:0.622 green:0.926 blue:1.000 alpha:1.000];


    [self.view addSubview:view1];

    

    

    

    view2 = [[UIView alloc]initWithFrame:CGRectMake(100+100, 100+100, 100, 100)];

    view2.backgroundColor = [UIColor colorWithRed:0.143 green:1.000 blue:0.312 alpha:1.000];

    

    [self.view addSubview:view2];

    

//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tagAction:)];

//    [view1 addGestureRecognizer:tap];

    

    

//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(snap:)];

//    [self.view addGestureRecognizer:tap];

    

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(attachments:)];

    [self.view addGestureRecognizer:tap];

}





#pragma mark-推送

-(void)tagAction:(UITapGestureRecognizer *)sender

{

    [dynamicAnimator removeAllBehaviors];

    

    UIPushBehavior *push = [[UIPushBehavior alloc]initWithItems:@[view1] mode:UIPushBehaviorModeContinuous];

//    x 正右   負左     y正下  負上

    push.pushDirection = CGVectorMake(1, 3);

  

//    push.active = NO;  //設置了就沒有效果了

    

//    推送的力度

    push.magnitude = 10;

    [dynamicAnimator addBehavior:push];


}



#pragma mark-快速移動

-(void)snap:(UITapGestureRecognizer *)sender

{

    [dynamicAnimator removeAllBehaviors];

    

    

//     迅速移動效果 只能一次 添加到一個元素上  snapToPoint 讓他移動到哪一個點

    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:view1 snapToPoint:[sender locationInView:self.view]];

    snap.damping = 0.1; //跳動的弧度   0-1

    

    [dynamicAnimator addBehavior:snap];


}



#pragma mark-吸附效果

-(void)attachments:(UITapGestureRecognizer *)sender

{

    [dynamicAnimator removeAllBehaviors];

    

//    offsetFromCenter:<#(UIOffset)#> 距離中心的偏移量

    UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(20, 20) attachedToAnchor:[sender locationInView:self.view]];

    

//    attach.length = 10;   //距離點擊點的位置

    attach.damping = 0.1;  //跳動的幅度

    attach.frequency = 10;

    

    [dynamicAnimator addBehavior:attach];

  

    

//    UIAttachmentBehavior *attach1 = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(50, 50) attachedToItem:view2 offsetFromCenter:UIOffsetMake(100, 100)];

//    attach1.length = 50;   //距離點擊點的位置

//    attach1.damping = 0.1;  //跳動的幅度

//    attach1.frequency = 10;

//    [dynamicAnimator addBehavior:attach1];

    

    

    UIAttachmentBehavior *attah2 = [[UIAttachmentBehavior alloc]initWithItem:view1 attachedToItem:view2];

    

     attah2.damping = 0.1;  //跳動的幅度

     attah2.frequency = 10;

    

    

    [dynamicAnimator addBehavior:attah2];

    

    UIPushBehavior *push = [[UIPushBehavior alloc]initWithItems:@[view1] mode:UIPushBehaviorModeContinuous];

    //    x 正右   負左     y正下  負上

    push.pushDirection = CGVectorMake(1, 3);

    

    //    push.active = NO;  //設置了就沒有效果了

    

    //    推送的力度

    push.magnitude = 10;

    [dynamicAnimator addBehavior:push];


    

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章