简体   繁体   中英

Using UICollisionBehavior for Setting boundaries

i am a beginner in ios programming. When i am using the UiCollisionBehavior to set the boundaries for the dropping items, i added the following code to the 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

however, the myView did not drop and just stayed at the original position. Can someone tell me the reason?

替换此行,您不会在UICollisionBehavior中添加障碍

_collision = [[UICollisionBehavior alloc] initWithItems:@[myView, barrier]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM