2014-04-17 4 views
0

고정 된 있지만 충돌 개체로 배치 할 수 있습니까? 내보기에서는 바닥에 떨어지는 상자보기가 바닥보기와 충돌합니다. 저는 바닥에 앉아서 그 위치에 머물러 있기를 원합니다. 그러나 그것이 상자에 부딪힌 후에 움직입니다. UIView (UIBezierPath 등)보다 개체를 확대하거나 다른 개체를 사용하고 싶지 않은데 한 곳에서 어떻게 붙일 수 있습니까? (아주 간단한 설치입니다) 문제에 대한충돌에 대한 UIView 수정

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIButton *start = [[UIButton alloc]initWithFrame:CGRectMake(200, 20, 40, 40)]; 
    start.backgroundColor = [UIColor redColor]; 
    [start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside]; 
    [main addSubview:start]; 

    self.box1 = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 20, 20)]; 
    self.box1.backgroundColor = [UIColor redColor]; 
    [main addSubview:self.box1]; 

    self.floor = [[UIView alloc]initWithFrame:CGRectMake(0, main.frame.size.height-20, main.frame.size.width, 200)]; 
    self.floor.backgroundColor = [UIColor lightGrayColor]; 
    [main addSubview:self.floor]; 
} 

- (void) start { 
    self.animation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view]; 
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[self.box1]]; 
    [self.animation addBehavior:gravity]; 

    UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[self.box1, self.floor]]; 
    [self.animation addBehavior:collision]; 
} 

답변

0

하나 개의 솔루션은 참조 뷰의 역할을 것이다 UIView의을 만들 수 있습니다. 보기의 맨 아래가 바닥이되도록 프레임을 설정하십시오. 그런 다음 해당 참조보기의 하위보기로 상자를 추가하고 translatesReferenceBoundsIntoBoundary을 YES로 설정하여 충돌 동작을 추가하면 상자가 바닥에 떨어집니다.

// Reference view 
CGRect referenceFrame = CGRectMake(0, 0, 320, 500); // the floor will be at y = 500 
UIView *referenceView = [[UIView alloc] initWithFrame:referenceFrame]; 
[self.view addSubview:referenceView]; 

// Box 
CGRect boxFrame = CGRectMake(50, 50, 10, 10); 
UIView *boxView = [[UIView alloc] initWithFrame:boxFrame]; 
boxView.backgroundColor = [UIColor redColor]; 

[referenceView addSubview:boxView]; 

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

// Gravity 
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[boxView]]; 
[self.animator addBehavior:gravity]; 

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[boxView]]; 
collision.translatesReferenceBoundsIntoBoundary = YES; 
[self.animator addBehavior:collision]; 

당신은 referenceView

0

의 내보기를 UIDynamicItemBehavior를 사용하여 외부 바닥에 꽤 시각을 제공하고 사실로 다음과 같은 속성을 설정할 수 있습니다.

@property(nonatomic, getter=isAnchored) BOOL anchored; 

이렇게하면 UIView이 잠 깁니다. 위 항목을 비헤이비어에 추가하고 비헤이비어를 애니메이터에게 추가하려면 위에서 수행 한 작업을 수행하십시오.