Чтобы продемонстрировать вам делегат в действии и показать, как его можно использовать, расширим приведенный пример. Как только квадратики достигают нижней границы опорного вида, мы делаем их красными, увеличиваем на 200 %, а потом заставляем рассыпаться, как при взрыве, и исчезать из виду:

NSString *const kBottomBoundary = @"bottomBoundary";

@interface ViewController  

@property (nonatomic, strong) NSMutableArray *squareViews;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end

@implementation ViewController

— (void)collisionBehavior:(UICollisionBehavior*)paramBehavior

beganContactForItem:(id )paramItem

withBoundaryIdentifier:(id )paramIdentifier

atPoint:(CGPoint)paramPoint{

NSString *identifier = (NSString *)paramIdentifier;

if ([identifier isEqualToString: kBottomBoundary]){

[UIView animateWithDuration:1.0f animations: ^{

UIView *view = (UIView *)paramItem;

view.backgroundColor = [UIColor redColor];

view.alpha = 0.0f;

view.transform = CGAffineTransformMakeScale(2.0f, 2.0f);

} completion: ^(BOOL finished) {

UIView *view = (UIView *)paramItem;

[paramBehavior removeItem: paramItem];

[view removeFromSuperview];

}];

}

}

— (void)viewDidAppearBOOL)animated{

[super viewDidAppear: animated];

/* Создаем виды */

NSUInteger const NumberOfViews = 2;

self.squareViews = [[NSMutableArray alloc] initWithCapacity: NumberOfViews];

NSArray *colors = @[[UIColor redColor], [UIColor greenColor]];

CGPoint currentCenterPoint = CGPointMake(self.view.center.x, 0.0f);

CGSize eachViewSize = CGSizeMake(50.0f, 50.0f);

for (NSUInteger counter = 0; counter < NumberOfViews; counter++){

UIView *newView =

[[UIView alloc] initWithFrame:

CGRectMake(0.0f, 0.0f, eachViewSize.width, eachViewSize.height)];

newView.backgroundColor = colors[counter];

newView.center = currentCenterPoint;

currentCenterPoint.y += eachViewSize.height + 10.0f;

[self.view addSubview: newView];

[self.squareViews addObject: newView];

}

self.animator = [[UIDynamicAnimator alloc]

initWithReferenceView: self.view];

/* Создаем тяготение */

UIGravityBehavior *gravity = [[UIGravityBehavior alloc]

initWithItems: self.squareViews];

[self.animator addBehavior: gravity];

/* Создаем обнаружение столкновений */

UICollisionBehavior *collision = [[UICollisionBehavior alloc]

initWithItems: self.squareViews];

[collision

addBoundaryWithIdentifier: kBottomBoundary

fromPoint: CGPointMake(0.0f, self.view.bounds.size.height — 100.0f)

toPoint: CGPointMake(self.view.bounds.size.width,

self.view.bounds.size.height — 100.0f)];

collision.collisionDelegate = self;

[self.animator addBehavior: collision];

}

Перейти на страницу:
Нет соединения с сервером, попробуйте зайти чуть позже