0

我是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 并没有下降,只是停留在原来的位置。有人可以告诉我原因吗?

4

1 回答 1

0

替换此行,您没有在 UICollisionBehavior 中添加障碍

_collision = [[UICollisionBehavior alloc] initWithItems:@[myView, barrier]];
于 2015-01-07T06:31:57.187 回答