iOS開發UIkit動力學UIDynamicAnimator一系列動畫

UIDynamicAnimator類,經過這個類中的不一樣行爲來實現一些動態特性。動畫

UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityBehavior(重力),UIPushBehavior(推進),UISnapBehavior(捕捉)。另外還有一個輔助的行爲UIDynamicItemBehavior,用來在item層級設定一些參數,好比item的摩擦,阻力,角阻力,彈性密度和可容許的旋轉等等。spa

1.UIAttachmentBehavior(吸附):code

- (void)viewDidLoad {
    [super viewDidLoad];

    self.dynamicAnimation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    // 圖片
    self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100,100)];
    self.imgView.image = [UIImage imageNamed:@"dizhijieyong_icon"];
    [self.view addSubview:self.imgView];
    self.imgView.userInteractionEnabled = YES;
    
    // 添加手勢
    UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestAction:)];
    [self.imgView addGestureRecognizer:panGest];
    
    // 吸附 offset能夠設置吸附的偏移,anchor是設置錨點
    self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:UIOffsetMake(100, 120) attachedToAnchor:CGPointMake(100, 100)];
}

// 實現一個pan手勢,讓一個image跟着手勢跑

- (void)panGestAction: (UIPanGestureRecognizer *)panGest{
    
    NSLog(@"手勢觸發啦!!!");
    
    // 取得location
    CGPoint location = [panGest locationInView:self.view];
    CGPoint boxLocation = [panGest locationInView:self.imgView];
    
    switch (panGest.state) {
            // 開始拖拽
        case UIGestureRecognizerStateBegan:
            
            NSLog(@"you touch started position %@",NSStringFromCGPoint(location));
            NSLog(@"location in image started is %@",NSStringFromCGPoint(boxLocation));
            // 移除
            [self.dynamicAnimation removeAllBehaviors];
            
            UIOffset offSet = UIOffsetMake(boxLocation.x - CGRectGetMidX(self.imgView.bounds),
                                           boxLocation.y - CGRectGetMidY(self.imgView.bounds));
            
            self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:offSet attachedToAnchor:location];
            self.attachment.damping=0.5;
            self.attachment.frequency=0.8;
            [self.dynamicAnimation addBehavior:self.attachment];
            break;
         case UIGestureRecognizerStateEnded:
            [self.dynamicAnimation removeAllBehaviors];
        default:
            [self.attachment setAnchorPoint:location];
            break;
    }
}

 

2.UIGravityBehavior(重力):blog

// 實現點擊屏幕掉落一張圖片的代碼

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    self.gravity = [[UIGravityBehavior alloc]initWithItems:nil];
    [self.dynamicAnimation addBehavior:self.gravity];
    
    UIImageView *gravImagView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
    gravImagView.image = [UIImage imageNamed:@"zichanlingyong_icon"];
    [self.view addSubview:gravImagView];
    gravImagView.frame = CGRectMake(100, 100, 100, 100);
    [self.gravity addItem:gravImagView];
    
}

 

3.UISnapBehavior(捕捉):圖片

// UISnapBehavior 將UIView經過動畫吸附到某個點上
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
    CGPoint tapPoint = [paramTap locationInView:self.view];
    
    if (self.snap != nil){
        [self.dynamicAnimation removeBehavior:self.snap];
    }
    self.snap = [[UISnapBehavior alloc] initWithItem:self.imgView snapToPoint:tapPoint];
    self.snap.damping = 0.5f;  //劇列程度
    [self.dynamicAnimation addBehavior:self.snap];
}

 

還有一些沒有試過,有時間補充吧!!!嘻嘻~~~~~rem

相關文章
相關標籤/搜索