前回は重力しか設定されていなかったので、落ち放題でした。
self.viewがアニメーションの範囲とはいえ、self.viewの下で止まってくれたりはしません。self.viewとredviewの間には何のBehaviorもなかったからです。
今回は落ちないようにしてみましょう。

UICollisionBehaviorはViewとViewが重ならないように設定するものです。これを使ってみましょう。
redViewの下にredViewとCollisionを設定した緑色のViewを置いてみます。

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    // target

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

    [redView setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:redView];

    

    // blocker

    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(10, 350, 300, 10)];

    [greenView setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:greenView];

    

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

    

    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[redView]];

    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[redView,greenView]];

    [self.animator addBehavior:gravityBeahvior];

    [self.animator addBehavior:collisionBehavior];

}


こんな感じ。
緑色のViewを用意してself.viewにつけました。
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior allocinitWithItems:@[redView,greenView]];
この部分でredViewとgreenViewにCollisionBehaviorが追加されます。つまり2つはぶつかることが出来ます。
これで落下する赤いViewを緑のViewで止めれるはず。やってみましょう


・・・見事に失敗です。
緑のViewは赤いのにつられて落ちてしまいました。重力は設定してないので勝手に落ちることはないですが、
どうも他のViewに押されたら落ちるみたいですね。
つまり「えっ、Collision設定したじゃん。ってことはぶつかるんだから赤いのと一緒に落ちるに決まってるじゃん」ということらしいです。
緑のViewときちんと固定しない限り赤いのを支えることは出来ないようです。
固定するBehaviorがあるのでそれを使ってみましょう。
UIAttachmentBehaviorはViewとView。またはViewとあるCGPoint同士を固定するBehaviorです。
沢山のViewをAttachmentBehaviorでつないでヘビみたいに引っ張ることも可能です。
これをつかって緑色のViewを固定してみましょう。

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    // target

    UIView *redView = [[UIView allocinitWithFrame:CGRectMake(100100100100)];

    [redView setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:redView];

    

    // blocker

    UIView *greenView = [[UIView allocinitWithFrame:CGRectMake(1035030010)];

    [greenView setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:greenView];

    

    self.animator = [[UIDynamicAnimator allocinitWithReferenceView:self.view];

    

    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior allocinitWithItems:@[redView]];

    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior allocinitWithItems:@[redView,greenView]];

    UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior allocinitWithItem:greenView attachedToAnchor:CGPointMake(160355)];

    [self.animator addBehavior:gravityBeahvior];

    [self.animator addBehavior:collisionBehavior];

    [self.animator addBehavior:attachmentBehavior];

}


こんな風にしてみました。
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior allocinitWithItem:greenView attachedToAnchor:CGPointMake(160355)];
greenViewに160,355(greenViewの中央)とのattachmentを追加しました。
ですが、実子すると面白い感じになっちゃいますね(笑)。これはこれで面白いんですがやりたいことと違うね。
確かに、1点で支えるのは普通に考えて無理ですね。大道芸人じゃあるまいし。
左右から支えてみましょう。

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    // target

    UIView *redView = [[UIView allocinitWithFrame:CGRectMake(100100100100)];

    [redView setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:redView];

    

    // blocker

    UIView *greenView = [[UIView allocinitWithFrame:CGRectMake(1035030010)];

    [greenView setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:greenView];

    

    UIView *subview1 = [[UIView allocinitWithFrame:CGRectMake(03603010)];

    [subview1 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:subview1];

    

    UIView *subview2 = [[UIView allocinitWithFrame:CGRectMake(2803603010)];

    [subview2 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:subview2];

    

    self.animator = [[UIDynamicAnimator allocinitWithReferenceView:self.view];

    

    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior allocinitWithItems:@[redView]];

    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior allocinitWithItems:@[redView,greenView,subview1,subview2]];

    UIAttachmentBehavior *leftAttachmentBehavior = [[UIAttachmentBehavior allocinitWithItem:subview1 attachedToAnchor:subview1.center];

    UIAttachmentBehavior *rightAttachmentBehavior = [[UIAttachmentBehavior allocinitWithItem:subview2 attachedToAnchor:subview2.center];

    [self.animator addBehavior:gravityBeahvior];

    [self.animator addBehavior:collisionBehavior];

    [self.animator addBehavior:leftAttachmentBehavior];

    [self.animator addBehavior:rightAttachmentBehavior];

}


greenViewの右下と左下に小さなViewを用意しました。
そしてそれらとgreenViewはcollisionがありぶつかります。
また、その小さなViewはcenterでattachmentしてあります。
これならgreenViewを支えられそうです。
実行してみるとうまくいきます。ただ、バウンドするので落ちちゃいそうですが、非常に自然ですね。

you can use pictures and texts on this site as you like.