動力效果,已然眼動力效果怎麼樣

來源:互聯網
上載者:User

動力效果,已然眼動力效果怎麼樣

動力效果的實現首先由一個UIDynamicAnimator動畫的繪製者 ,給他一個UIDynamicBehavior動力效果 、 把UIDynamicItem(UIView)動力元素添加到動力效果上

動力元素:只要遵守了UIDynamicItem這個協議的都可以看成動力行為的元素

UIDynamicAnimator動畫的繪製者 需要一個作用的地區範圍()

他們的關係結構圖如下:

動力行為:

    1.簡介:屬於UIKit

        (1)什麼是動力行為? -> 類比真實世界中力學相關的動畫和互動系統

        (2)可以實現的效果: -> 重力、碰撞、吸附、推動、捕捉效果、並且可以組合

        (3)iOS9 UIDynamicItemGroup可以統一給一組元素添加動力行為

        (4)iOS9 可以定義成 以球形方式 去接觸 另一個邊境 或元素

    2.類名介紹:

        (1)UIDynamicAnimator:動力效果的動畫播放者

    屬性介紹:

              ①initWithReferenceView:初始化動力效果播放者 並制定參考視圖

              ②addBehavior:添加動力行為

              ③removeBehavior:移除某個動力行為

              ④removeAllBehaviors 移除所有動力行為

              ⑤delegate 代理

        (2)UIDynamicBehavior:動力效果的動畫行為  動力效果的動畫行為會影響到動力元素的屬性(frame)

    動力行為分六大行為:

            《1》UIGravityBehavior        重力效果行為

                ①initWithItems:初始化重力效果行為 並指定作用對象

                ②addItem:添加重力效果作用對象

                ③removeItem:移除作用對象

                ④gravityDirection重力的方向

                 CGVector:表示向量的結構體 值:0-1

                 x:橫向重力

                 y:豎向重力

                 受加速度、弧度的影響 每秒下降1000個像素點

                ⑤angle:更改重力效果的角度

                ⑥magnitude:加速度的層級 1.0代表加速度是1000 points /second²

                ⑦setAngle:magnitude:設定重力方向的角度 和速度

            《2》UICollisionBehavior      碰撞行為

                ①initWithItems:初始化

                ②collisionMode:碰撞樣式(模式)

                     UICollisionBehaviorModeItems      元素碰撞

                     UICollisionBehaviorModeBoundaries 邊境碰撞

                     UICollisionBehaviorModeEverything 所有都可以碰撞

                ③collisionDelegate:代理

                ④元素碰撞的時候調用的代理方法

                 - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;

                 - (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;

                ⑤元素與邊境之間碰撞的時候調用的代理方法

                 - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;

                 - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;

                ⑥addBoundaryWithIdentifier:forPath: 添加路徑 為邊境

                ⑦- (nullable UIBezierPath *)boundaryWithIdentifier:(id <NSCopying>)identifier

                ⑧addBoundaryWithIdentifier:fromPoint:toPoint: 添加一條線為邊境

                ⑨removeBoundaryWithIdentifier

                ⑩translatesReferenceBoundsIntoBoundary 是否以參照視圖作為邊界

                ⑪setTranslatesReferenceBoundsIntoBoundaryWithInsets:設定參照視圖的內間距

            《3》UIPushBehavior           推動行為

            《4》UISnapBehavior           迅速移動

            《5》UIAttachmentBehavior     附和行為

            《6》UIDynamicItemBehavior    元素行為

  (3)UIDynamicItem動力元素

以上就是關於動力行為的介紹,具體操作如下:

首先在Main.sroryBoard裡面建立一個UIImageView,設定屬性全屏、允許與使用者互動、image。同時把ImageView拖到ViewController.m檔案

@interface裡面

#import "ViewController.h"@interface ViewController ()<UIDynamicAnimatorDelegate,UICollisionBehaviorDelegate>//動力效果的元素@property(nonatomic,strong)UIImageView *ballItem;@property (weak, nonatomic) IBOutlet UIImageView *bgView;@property(nonatomic,strong)UIDynamicAnimator *animatior;//碰撞到邊境出現的圖片@property(nonatomic,strong)UIImageView *dungView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    [self.bgView addSubview:self.ballItem];}- (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 gravityBehavior];    [self collisionBehavior];}#pragma mark-------重力效果- (void)gravityBehavior{    //移除之前的效果    [self.animatior removeAllBehaviors];    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[self.ballItem]];    gravityBehavior.gravityDirection = CGVectorMake(0.0, 1.0);    //重力的方向    //gravityBehavior.angle = 30*M_PI/180;    //加速度 會影響下降速度    gravityBehavior.magnitude = 1.5;    //把重力效果添加到動力效果的操縱者上    [self.animatior addBehavior:gravityBehavior];}#pragma mark-----檢測碰撞的行為- (void)collisionBehavior{        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];        //畫線    [collisionBehavior addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 680) toPoint:CGPointMake(414, 680)];    collisionBehavior.collisionDelegate = self;    [self.animatior addBehavior:collisionBehavior];}#pragma mark--------碰撞行為的代理方法//元素之間的碰撞- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p{    }- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2{    }//元素與邊境之間的碰撞- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p{    NSLog(@"開始接觸邊境");        self.dungView.center = CGPointMake(p.x-20, p.y-20);    //[self.dungView addSubview:self.ballItem];    [self.bgView exchangeSubviewAtIndex:1 withSubviewAtIndex:0];}- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier{    NSLog(@"結束接觸邊境");        [behavior removeBoundaryWithIdentifier:@"line"];    [self collisionBehavior];}#pragma mark--------UIDynamicAnimatorDelegate(動力效果操縱者)的兩個代理方法- (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 *)animatior{    if (_animatior) {        return _animatior;    }    _animatior = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];    _animatior.delegate = self;    return _animatior;}- (UIImageView *)dungView{    if (_dungView) {        return _dungView;    }    UIImage *image = [UIImage imageNamed:@"keng"];    _dungView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 680, image.size.width/2.5, image.size.height/2.5)];    _dungView.image = image;    [self.bgView addSubview:_dungView];        return _dungView;}@end

啟動並執行效果就是當你拖動完之後小球掉落會出現一個坑。、

效果如下:

            初始位置

           掉落完成的狀態

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.