使用UICollisionBehavior设置边界

时间:2015-01-07 05:38:13

标签: ios uicollisionbehavior

我是ios编程的初学者。当我使用UiCollisionBehavior设置删除项的边界时,我将以下代码添加到ViewController.m

#import "ViewController.h"


@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    myView.backgroundColor = [UIColor grayColor];
    UIView *barrier = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 130, 20)];
    barrier.backgroundColor = [UIColor redColor];
    [self.view addSubview: barrier];
    [self.view addSubview:myView];
    _animator = [[UIDynamicAnimator alloc]initWithReferenceView:myView];
    _gravity = [[UIGravityBehavior alloc]initWithItems:@[myView]];
    [_animator addBehavior:_gravity];
    _collision = [[UICollisionBehavior alloc]
                  initWithItems:@[myView]];
    _collision.translatesReferenceBoundsIntoBoundary= YES;
    [_animator addBehavior:_collision];

 }

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}

@end

然而,myView没有下降,只是停留在原来的位置。有人可以告诉我原因吗?

1 个答案:

答案 0 :(得分:0)

替换此行,您没有在UICollisionBehavior

中添加障碍
_collision = [[UICollisionBehavior alloc] initWithItems:@[myView, barrier]];