LoginSignup
6
8

More than 5 years have passed since last update.

UIDynamicAnimatorを用いた重力と衝突のメモ

Last updated at Posted at 2014-11-04

起動時にviewが画面下ギリギリでバウンドするアニメーション

ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIDynamicAnimatorDelegate>

// アニメーションするview
@property (nonatomic , strong) IBOutlet UIView *animationView;
@property (nonatomic , strong) UIDynamicAnimator *animator;

@end
ViewController.m
#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // self.viewを参照にしたアニメーションの生成
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];

    // animationViewに重力を設定
    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.animationView]];
    // 1.0倍の重力
    gravityBeahvior.magnitude = 1.0;
    // アニメーションに重力を追加
    [self.animator addBehavior:gravityBeahvior];


    // 衝突の設定
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.animationView]];
    // 参照との境界で境界線を引く
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    // アニメーションに衝突を追加
    [self.animator addBehavior:collisionBehavior];

}

@end
6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8