I read two questions here about that, both with the same answer. The suggested solution was to create the UIScrollView object, and then add it to the parent view of the SKScene in the target scene's didMoveToView:, removing it in willMoveFromView:.
And that is the target scene code I implemented:
#interface MQSSMakerScene ()
#property BOOL contentCreated;
#property MQSSMakerWorkspaceLayer *workspaceLayer;
#property MQSSMakerDockLayer *dockLayer;
#property UIScrollView *scrollView;
#end
#implementation MQSSMakerScene
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
CGSize layerSize = CGSizeMake(944, 140);
CGPoint layerPosition = CGPointMake(40, 768-(140+30));
CGRect viewFrame = CGRectMake(layerPosition.x, layerPosition.y, layerSize.width, layerSize.height);
_scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
_scrollView.contentSize = CGSizeMake(2000, 120);
_scrollView.scrollEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.backgroundColor = [UIColor brownColor];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
if(!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
[self.view addSubview:_scrollView]; // --> WHERE IT'S BEING ADDED
}
- (void)willMoveFromView:(SKView *)view
{
[super willMoveFromView:view];
[_scrollView removeFromSuperview]; // --> WHERE IT'S BEING REMOVED
}
- (void)createSceneContents
{
self.backgroundColor = [UIColor colorWithHue:.237 saturation:.56 brightness:.46 alpha:1];
self.scaleMode = SKSceneScaleModeAspectFill;
[self addChild:[self newMakerLayout]];
}
- (SKNode *)newMakerLayout
{
SKNode *mainLayout = [[SKNode alloc] init];
self.workspaceLayer = [[MQSSMakerWorkspaceLayer alloc] init];
self.dockLayer = [[MQSSMakerDockLayer alloc] init];
[mainLayout addChild:self.workspaceLayer];
[mainLayout addChild:self.dockLayer];
MQSSStoryObject *ob1 = [[MQSSStoryObject alloc] initWithImageNamed:#"maker-1.png"];
[self.workspaceLayer addChild:ob1];
return mainLayout;
}
#end
Please note that the user should go to this scene from the home scene when clicking on a button. The problem now is that once I add the line:
[self.view addSubview:_scrollView];
to add the UIScrollView object to this scene, the application no more goes to this scene and instead add the UIScrollView to the home scene I am transitioning from. It also doesn't remove it when transitioning to another scene. Once I comment out this line, everything works just as expected, clearly without presenting the UIScrollView.
I am stuck. Any help or advice is highly appreciated. Thanks!
I finally figured out what the problem was for me. I was using the -(void)willLayoutSubviews method of the viewController to ensure I had the correct bounds, but I had forgotten to see if my SKScene was already presented. That resulted in a new welcomeScene being presented each time the view was laid out on top of the old one.
-(void)willLayoutSubviews
{
if(!self.didPresentScene)
{
// Present scene here
....
self.didPresentScene = YES;
}
}
Related
My problem with after pinch zoom, when I pinch zoom and after drag image then white space on top and bottom.
My Code as per apple documentation.
https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html
Sample code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
scrollView.minimumZoomScale=0.5;
scrollView.maximumZoomScale=6.0;
scrollView.contentSize=CGSizeMake(1280, 960);
scrollView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
Thank you.
At first set your minimum zoom scale to 1.0:
scrollView.minimumZoomScale = 1.0;
Next, set the contentSize for scrollView:
scrollView.contentSize = [UIScreen mainScreen].bounds.size;
And if you don't want to see the extra space when bouncing, add the next line:
scrollView.bounces = NO;
Completely, the method should looks something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.minimumZoomScale = 1.0;
self.scrollView.maximumZoomScale = 6.0;
self.scrollView.bounces = NO;
self.scrollView.contentSize = [UIScreen mainScreen].bounds.size;
self.scrollView.delegate = self;
}
Hope it will help.
This is how I do it. If you image fits perfectly into the view, there will be no white space when you zoom and pan.
If your image does not fit or scale properly, there will be white space on the sides OR the top.
First make sure your image is the correct size. Next you need to add minimumZoomScale and maximumZoomScale to the scrollView. The rest will work after that. You can use auto-layout or frames. Up to you.
This example uses auto-layout:
#import "ViewController.h"
#interface AutoLayoutScrollView : UIScrollView
#property (nonatomic, weak) UIView *contentView;
#end
#implementation AutoLayoutScrollView
- (instancetype)init {
if (self = [super init]) {
UIView *view = [[UIView alloc] init];
self.contentView = view;
[self addSubview:view];
[self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor].active = YES;
[self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
[self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
[self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return self;
}
- (void)didMoveToSuperview {
[super didMoveToSuperview];
UIView *parent = self.superview;
if (parent) {
[self.leftAnchor constraintEqualToAnchor:parent.leftAnchor].active = YES;
[self.rightAnchor constraintEqualToAnchor:parent.rightAnchor].active = YES;
[self.topAnchor constraintEqualToAnchor:parent.topAnchor].active = YES;
[self.bottomAnchor constraintEqualToAnchor:parent.bottomAnchor].active = YES;
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
}
}
#end
#interface ViewController () <UIScrollViewDelegate>
#property (nonatomic, strong) AutoLayoutScrollView *scrollView;
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[AutoLayoutScrollView alloc] init];
[self.view addSubview:self.scrollView];
[self.scrollView.contentView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;
[self.scrollView.contentView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor].active = YES;
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Image"]];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.scrollView.contentView addSubview:self.imageView];
[self.imageView.leftAnchor constraintEqualToAnchor:self.scrollView.contentView.leftAnchor].active = YES;
[self.imageView.rightAnchor constraintEqualToAnchor:self.scrollView.contentView.rightAnchor].active = YES;
[self.imageView.topAnchor constraintEqualToAnchor:self.scrollView.contentView.topAnchor].active = YES;
[self.imageView.bottomAnchor constraintEqualToAnchor:self.scrollView.contentView.bottomAnchor].active = YES;
[self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView setMinimumZoomScale:1.0];
[self.scrollView setMaximumZoomScale:3.0];
[self.scrollView setZoomScale:1.0f animated:YES];
[self.scrollView setDelegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
#end
I have couple of UIButtons on the screen. If a pinch gesture occurs, I want to decrease the size of the buttons (Like a zoom-out effect) and add more buttons. How would I implement it?
I am typing this directly on StackOverflow, so there may be typos.
These features are left as an exercise for the OP:
Cumulatively scaling down with successive pinches. You must have another private property to hold the current scale value.
Adding new buttons after the zooming out effect.
Code:
#interface MyViewController : UIViewController
#property(nonatomic, strong) NSMutableArray* buttons;
- (void)pinched:(UIPinchGestureRecognizer*)gesture;
#end
#implementation MyViewController
- (void)loadView {
[super loadView];
self.buttons = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(
0.0f,
(44.0f + 10.0f) * (CGFloat)i,
100.0f,
44.0f
)];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[self.buttons addObject:button];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinched:)];
[self.view addGestureRecognizer:pinch];
}
- (void)pinched:(UIPinchGestureRecognizer*)gesture {
if (gesture.scale > 1.0) {
return;
}
for (UIButton* button in self.buttons) {
[UIView
animateWithDuration:1.0
animations:^void() {
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
];
}
}
#end
I am trying to do a simple scroll, but the views do not move after a touch, I am not sure why, the scrollview should handle the gesture, but something might be missing. Would someone know where?
Here is the code : I create a small horizontal scroll view, with some views inside. The views appear well, I am testing it on a device for the touch :
- (void)viewDidLoad {
[super viewDidLoad];
//horizontal scroll view
HorizScroll *ho = [[HorizScroll alloc] initWithFrame:CGRectMake(0, 0, 500, 100)];
for ( int i=0; i<3; i++){
MyView* mv = [[MyView alloc] init];
[ho addSubview:mv];
}
//ho.zoomScale = 0.3f;
[self.view addSubview:ho];
}
#implementation HorizScroll
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
-(void)addSubview:(UIView *)view{
[super addSubview:view];
NSUInteger numSubviews = self.subviews.count;
[view setFrame:CGRectMake(CGRectGetWidth(view.bounds)*(numSubviews),
0,
CGRectGetWidth(view.bounds),
CGRectGetHeight(view.bounds) )];
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds) )];
}
#implementation MyView
-(int)getRandomNumberBetween:(int)from to:(int)pto {
return (int)(from + arc4random() % (pto-from+1));
}
-(instancetype)init{
if ( self = [super init] ){
CGFloat red = [self getRandomNumberBetween:1 to:255];
self.backgroundColor = [UIColor colorWithRed:red/256.0
green:[self getRandomNumberBetween:1 to:255]/256.0
blue:[self getRandomNumberBetween:1 to:255]/256.0
alpha:1.0];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if ( self = [super initWithFrame:frame] ){
self.frame = CGRectMake(counterX, 0, 100, 100);
counterX += 50;
}
return self;
}
You need to set contentSize of scrollview to be larger than its frame size. so add this line after [self.view addSubview:ho].
ho.contentSize = CGSizeMake(501.f, 100.f);
or before [self.view addSubview:ho] and comment out the line:
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds))];
which is not necessary since you can set it after all subviews are added.
I have two images: first image size is 50x85 and second image size is 50x113. When I touch the screen image goes up and second image is shown and when I release the screen image goes down and first image is shown. Now the problem is when I'm touching the screen collision happens earlier than when I'm not touching the screen.
How could I make that in second image about 30px at bottom won't collide, but image will still be there?
Edit: Here is some of the code:
-(void)AstronautMove
{
[self Collision];
Astronaut.center = CGPointMake(Astronaut.center.x, Astronaut.center.y + Y);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
Y = -10;
x.image = [UIImage imageNamed:#"y.png"];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
Y = 17;
x.image = [UIImage imageNamed:#"x.png"];
}
-(void)Collision
{
if (CGRectIntersectsRect(Astronaut.frame, Landingarea.frame)) {
[self EndGame];
AstronautFire.hidden = YES;
RedBorder.hidden = NO;
AstronautCrashed.hidden = NO;
}
if (CGRectIntersectsRect(AstronautFire.frame, Landingarea.frame)) {
[self EndGame];
AstronautFire.hidden = YES;
RedBorder.hidden = NO;
AstronautCrashed.hidden = NO;
}
}
I'm just moving the frame up and down. At the bottom is landing area. When Astronaut and landing area frames touch, collision happens. But when I'm moving Astronaut up bigger image is shown (50x113) and when I'm moving Astronaut down smaller image is shown (50x85). When I'm near landing area and I press to go up bigger image shows and collision happens.
From the code samples, it really is hard to follow what you're trying to do. Notably, it's unclear how (or even if) you're animating the moving of the various views.
Whether you're animating or not, it's not clear from your code sample as to where AstronautMove is called. Did you mean to call that from touchesBegan and touchesEnded? But on the basis of what's been shared thus far, I don't see you calling AstronautMove and therefore don't see how you could possibly detect collisions.
Personally, I would have thought that you'd want to animate the moving of the views (it will render a more realistic and engaging moving of the views). And then, while an animation is underway, keep checking for a collision. In terms of how to "keep checking", you can use either a NSTimer or, even better, us a CADisplayLink (a mechanism to call a method each and every time the screen updates).
For example, you could do something like:
#interface ViewController ()
#property (nonatomic, strong) UIView *view1;
#property (nonatomic, strong) UIView *view2;
#property (nonatomic, strong) CADisplayLink *displayLink;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
view1.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:view1];
self.view1 = view1;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 50, 50)];
view2.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:view2];
self.view2 = view2;
}
#pragma mark - Handle touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self startAnimationTo:CGPointMake(50, 250) collisionChecking:YES];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self startAnimationTo:CGPointMake(50, 50) collisionChecking:NO];
}
#pragma mark - Handle starting and stopping of animation
- (void)startAnimationTo:(CGPoint)point collisionChecking:(BOOL)collisionChecking
{
if (collisionChecking)
[self startDisplayLink];
[UIView animateWithDuration:2.0 delay:0.0 options:0 animations:^{
CGRect frame = self.view1.frame;
frame.origin = point;
self.view1.frame = frame;
} completion:^(BOOL finished) {
// in case we never have collision, let's make sure we stop the display link
if (collisionChecking)
[self stopDisplayLink];
}];
}
- (void)stopAnimation
{
CALayer *layer = self.view1.layer.presentationLayer;
[self.view1.layer removeAllAnimations];
self.view1.frame = layer.frame;
[self stopDisplayLink];
}
#pragma mark - Handle the display link & check for collisions
- (void)startDisplayLink
{
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(checkForCollision:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)stopDisplayLink
{
[self.displayLink invalidate];
self.displayLink = nil;
}
- (void)checkForCollision:(CADisplayLink *)displayLink
{
// Note, when checking for collision while an animation is underway, the `frame` of the
// view represents the final location of the view, not the current location. To get the
// current location, one has to grab the `presentationLayer`. So, we're going to get the
// `presentationLayer` of each of the views and use those to determine a collision.
CALayer *layer1 = self.view1.layer.presentationLayer;
CALayer *layer2 = self.view2.layer.presentationLayer;
if (CGRectIntersectsRect(layer1.frame, layer2.frame)) {
[self stopAnimation];
}
}
#end
Or, in iOS 7, you can use UIKit Dynamics to animate the movement of the views, identifying collisions programmatically using a UICollisionBehavior and assigning a collisionDelegate:
#interface ViewController () <UICollisionBehaviorDelegate>
#property (nonatomic, strong) UIView *view1;
#property (nonatomic, strong) UIView *view2;
#property (nonatomic, strong) UIDynamicAnimator *animator;
#property (nonatomic, strong) UISnapBehavior *snap;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
view1.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:view1];
self.view1 = view1;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 50, 50)];
view2.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:view2];
self.view2 = view2;
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// create behavior that will detect collisions, calling the `UICollisionBehaviorDelegate`
// methods if and when collisions are detected
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:#[view1, view2]];
collision.translatesReferenceBoundsIntoBoundary = YES;
collision.collisionDelegate = self;
[self.animator addBehavior:collision];
// in this example, I'm going to fix `view2` where it is, so it doesn't move when
// `view1` collides with it
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.view2 attachedToAnchor:self.view2.center];
[self.animator addBehavior:attachment];
// finally, let's specify that neither view1 nor view2 should rotate when
// snapping, colliding, etc.
UIDynamicItemBehavior *noRotate = [[UIDynamicItemBehavior alloc] initWithItems:#[view1, view2]];
noRotate.allowsRotation = NO;
[self.animator addBehavior:noRotate];
}
- (void)addSnap:(CGPoint)point
{
[self removeSnap];
self.snap = [[UISnapBehavior alloc] initWithItem:self.view1 snapToPoint:point];
[self.animator addBehavior:self.snap];
[self.animator updateItemUsingCurrentState:self.view1];
}
- (void)removeSnap
{
if (self.snap) {
[self.animator removeBehavior:self.snap];
self.snap = nil;
}
}
#pragma mark - Handle touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self addSnap:CGPointMake(75, 225)];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self addSnap:CGPointMake(75, 75)];
}
#pragma mark - UICollisionBehaviorDelegate
// you can use these to be informed when there is a collision
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
{
NSLog(#"%s", __FUNCTION__);
[self removeSnap];
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 atPoint:(CGPoint)p
{
NSLog(#"%s", __FUNCTION__);
[self removeSnap];
}
#end
Or, for a game, SpriteKit might be even better. You can google "SpriteKit example" and you'll probably find plenty of good introductions to SpriteKit.
I know that neither of the above examples are quite what you're doing, but hopefully it illustrates a couple of techniques for identifying collisions, which you could adapt for your app.
I'm using UIDynamics in my app. my app has two squares. one is fixed and the other can be moved (by applying a pan gesture). when i try to collide them they don't collide. the delegate methods never get called. here's my code and i hope someone can point out the problem.
UIDynamicAnimator* animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self.square1, self.square2]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[self.square1 addGestureRecognizer:pan];
[collisionBehavior setCollisionMode:UICollisionBehaviorModeEverything];
[animator addBehavior:collisionBehavior];
collisionBehavior.collisionDelegate = self;
Make animator as a property or instance variable. otherwise it will
be cleared after view load.
Make sure implemented UICollisionBehaviorDelegate protol and correspond methods
Here is my works implementation based on your code.
Interface:
#import <UIKit/UIKit.h>
#interface DynamicViewController : UIViewController<UICollisionBehaviorDelegate>
#property (strong, nonatomic) UIDynamicAnimator *animator;
#property (strong, nonatomic) UIPushBehavior *pushBehavior;
#end
Implementation:
#synthesize pushBehavior = _pushBehavior;
#synthesize animator = _animator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *square1 = [[UIView alloc] initWithFrame:CGRectMake(100, 30, 30, 30)];
square1.center = CGPointMake(150, 150);
square1.backgroundColor = [UIColor greenColor];
UIView *square2 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 30, 30)];
square2.center = CGPointMake(250, 350);
square2.backgroundColor = [UIColor redColor];
[self.view addSubview:square1];
[self.view addSubview:square2];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[square1, square2]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[collisionBehavior setCollisionMode:UICollisionBehaviorModeEverything];
collisionBehavior.collisionDelegate = self;
[_animator addBehavior:collisionBehavior];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[square1 addGestureRecognizer:pan];
}
#pragma mark push behavior
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan){
if (_pushBehavior) {
[_animator removeBehavior:_pushBehavior];
}
_pushBehavior = [[UIPushBehavior alloc] initWithItems:#[gesture.view] mode:UIPushBehaviorModeContinuous];
[_animator addBehavior:_pushBehavior];
}
CGPoint offset =[gesture translationInView:self.view];
_pushBehavior.pushDirection = CGVectorMake(offset.x,offset.y);
_pushBehavior.magnitude = sqrtf(offset.x * offset.x + offset.y * offset.y) / 50;
if (gesture.state == UIGestureRecognizerStateEnded) {
[_animator removeBehavior:_pushBehavior];
_pushBehavior = nil;
}
}
#pragma mark Colision delegate
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
{
NSLog(#"Colision Began");
}
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;
{
NSLog(#"Colision End");
}
Your UIDynamicAnimator* animator is destroyed when function ends due to ARC. Thats why you are not able to see any animations. Make it a instance variable and you will be fine.
I think there is some bug with UIDynamics (but not 100% sure let me know if I'm wrong).
I couldn't make my dynamic works until I created instance variables for UIDynamicAnimator and UIGravityBehavior.
Try add instance variable to the .m file in class extension as:
UIDynamicAnimator* animator;
UICollisionBehavior* collisionBehavior;
And change your first two line of code to:
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[self.square1, self.square2]];
It should help.
You have to specify the bounds using "addBoundaryWithIdentifier" for the collision class and remove the non-moving item form the collision