scale, rotate and move with UIGestureRecognizer simultaneously - ios

I can't find out why my scale, rotate and move won't work at the same time..
I have looked at a lot of examples and i can't seem to find the problem.
firstly I thought that is was a gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer problem, but it wasn't please help :)
here is my code:
#implementation StoryEditorPageHolderView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin{
self = [super initWithFrame:CGRectZero];
if(self){
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:property.imageName]];
self.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
[self addSubview:_imageView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[self addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[self addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:tapProfileImageRecognizer];
}
return self;
}
- (void)rotate:(UIRotationGestureRecognizer *)rotate {
if (rotate.state == UIGestureRecognizerStateBegan) {
prevRotation = 0.0;
}
float thisRotate = rotate.rotation - prevRotation;
prevRotation = rotate.rotation;
self.transform = CGAffineTransformRotate(self.transform, thisRotate);
}
- (void)scale:(UIPinchGestureRecognizer *)pinch {
if (pinch.state == UIGestureRecognizerStateBegan)
prevPinchScale = 1.0;
float thisScale = 1 + (pinch.scale-prevPinchScale);
prevPinchScale = pinch.scale;
self.transform = CGAffineTransformScale(self.transform, thisScale, thisScale);
}
-(void)move:(UIPanGestureRecognizer *)pan {
if (pan.state == UIGestureRecognizerStateBegan){
prevPanPoint = [pan locationInView:self.superview];
}
CGPoint curr = [pan locationInView:self.superview];
float diffx = curr.x - prevPanPoint.x;
float diffy = curr.y - prevPanPoint.y;
CGPoint centre = self.center;
centre.x += diffx;
centre.y += diffy;
self.center = centre;
prevPanPoint = curr;
}
#end
I also have UIGestureRecognizerDelegate as a delegate in the .h file:
#import <Foundation/Foundation.h>
#class Property;
#class DustbinView;
#interface StoryEditorPropertyView : UIView <UIGestureRecognizerDelegate>
#property (strong, nonatomic) UIPanGestureRecognizer *panRecognizer;
#property (strong, nonatomic) UIPinchGestureRecognizer *pinchRecognizer;
#property (strong, nonatomic) UIRotationGestureRecognizer *rotationRecognizer;
#property (strong, nonatomic) UITapGestureRecognizer *tapProfileImageRecognizer;
#property (strong, nonatomic) UIImageView *imageView;
#property (strong, nonatomic) Property *property;
#property (nonatomic) CGPoint pointBegin;
#property (nonatomic) bool isRemoveable;
#property (nonatomic) CGFloat beginScale;
#property (strong, nonatomic) DustbinView *dustBin;
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin;
#end

This worked for me:
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
pinchRecognizer.delegate = self;
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];

ok, what you're doing wrong is not adding an object for the UIGestureRecognizer to adapt to. For example, you have the
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:tapProfileImageRecognizer];
This is exactly what you need except this line
[self addGestureRecognizer:tapProfileImageRecognizer];
this is adding the gesture onto the class. you need an OBJECT :)
so just do this
[self.view addGestureRecognizer:tapProfileImageRecognizer];
for all your gestures, and it should work :)
You always have to make sure you're adding onto the object, not the class
Hope this helps!!
EDIT:
Putting UIGestureRecognizers onto UIView is the way that you would do what you are doing. What you need to do now is go under Xcode,
file -> new file
make a UIViewController subclass
make sure that name it...
under your .xib file, find this:
Then, if you look at where the UIView is, click that. This will bring up the UIView attributes panel. Travel over to the little button that has the i in the circle. It's the blue one under the Attributes panel. Click that, and you should see this:
Where you see the box that says 'UIView'
That is where you type your subclass of UIView.
That should help with any of the issues you have been having. Hope that helps! :)

adding gesture code you should put in viewDidLoad not in init method and add gesture in view(self.view) not in object(self) itself .
try as follow I made some changes
-(void) viewDidLoad
{
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[self.view addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[self.view addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapProfileImageRecognizer];
}

Related

UIImageView is not removed from Superview in ObjectiveC

So , i have this void where i initialize the GUI and remove it when it's clicked the play image i call it with [self GUI:1]; and [self GUI:0];
The GUI comes up but when i try to hide it it's not working but it's entering in the if()
-(void)GUI:(int)action{
// GUY LOADING
UIImageView *menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
UIImageView *menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
UIImageView *menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menu.image=[UIImage imageNamed:#"menustart.png"];
menuplay.image=[UIImage imageNamed:#"menuplay.png"];
menuretry.image=[UIImage imageNamed:#"retrymenu.png"];
if(action == 1){
[self.view addSubview:menu];
[self.view addSubview:menuplay];
[self.view addSubview:menuretry];
}
if(action == 0){
[menu removeFromSuperview];
[menuplay removeFromSuperview];
[menuretry removeFromSuperview];
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
the StartGame selector only executes the following code
[self GUI:0];
and the viewDidLoad only executes the following code
[self GUI:1];
Every time the methode GUI: is called, no it not called a void you create new instance of the imageviews. Also GUI is not a good name for a method, better would be something like: setMenuVisible:
Since there is no reference to the old, previous image view they can not removed, You need to keep a reference to the image views.
So in your header do the following:
#property (strong, nonatomic) UIImageView *menu;
#property (strong, nonatomic) UIImageView *menuplay;
#property (strong, nonatomic) UIImageView *menuretry;
Then in you GUI: methode:
-(void)GUI:(int)action{
// GUY LOADING
if (!self.menu) {
self.menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
menu.image=[UIImage imageNamed:#"menustart"];
}
if (!self.menuplay) {
self.menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
menuplay.image=[UIImage imageNamed:#"menuplay"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
if (!self.menuretry) {
self.menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menuretry.image=[UIImage imageNamed:#"retrymenu"];
}
if(action == 1){
[self.view addSubview:self.menu];
[self.view addSubview:self.menuplay];
[self.view addSubview:self.menuretry];
}
else if(action == 0){
[self.menu removeFromSuperview];
[self.menuplay removeFromSuperview];
[self.menuretry removeFromSuperview];
}
}

How can i pass label text to textview in ios

I have tried every thing I could do to solve the problem because I am newbie I am not getting the clear answer.
This is my first View controller
#implementation ViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"textAdd"]) {
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
else{
tv.usingText.text=self.text;
}
}
}
-(IBAction)GetttingText:(UIStoryboardSegue*)segue
{
TextAddViewController *getText=segue.sourceViewController;
[self AddTextToCanvasWithGesture:getText.usingText];
}
-(void) LaunchText
{
[self performSegueWithIdentifier:#"textAdd" sender:self];
}
-(void)AddTextToCanvasWithGesture:(UITextView*)takenText
{
CGRect rect = CGRectMake(35, 60,takenText.contentSize.width,takenText.contentSize.height);
UIView *holderView = [[UIView alloc] initWithFrame:rect];
rect.origin.x=0;
rect.origin.y=0;
UILabel *textLabel=[[UILabel alloc]initWithFrame:rect];
[textLabel setText:takenText.text];
self.text=takenText.text;
[textLabel setTextColor:takenText.textColor];
[textLabel setFont:[UIFont fontWithName:takenText.font.fontName size:takenText.font.pointSize]];
[textLabel setNumberOfLines:0];
[textLabel sizeToFit];
[holderView setTag:1];
[textLabel setTag:1+1];
[holderView addSubview:textLabel];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
pinchRecognizer = nil;
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
rotationRecognizer = nil;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
panRecognizer = nil;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
tapRecognizer = nil;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(LaunchText)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[doubleTapRecognizer setDelegate:self];
[holderView addGestureRecognizer:doubleTapRecognizer];
holderView.userInteractionEnabled=YES;
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
}
And This Is my Secondview contoller
#import "TextAddViewController.h"
#import "ViewController.h"
#interface TextAddViewController ()
{
}
#property (weak, nonatomic) IBOutlet UILabel *noting;
#end
#implementation TextAddViewController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *vm=[[ViewController alloc]init ];
// [self.noting setText:#"Hi"];
[self.noting setText:vm.ShowText.text];
[self.usingText setText:vm.text];
}
My code runs perfect for displaying text of textview on Label but i want to edit text of label again so I have to display the text of label in same textview. Problem is it is not showing text in textview. every time textview is empty.
You are doing a couple of things wrong. The main thing is that you instantiate a new instance of your TextAddViewController. If you are new to object oriented programming this might be a bit difficult to understand (I suggest following this link (youtube) and learning a bit more before continuing on with xcode).
You correctly call the prepareForSegue method, which will fetch the storyboard segue you have created, and tag any data along that seg to your destinationViewController. Your problem is that you don't actually fetch your destinationViewController. Instead you are creating a new instance of TextAddViewController that will just float around in memory doing nothing.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"textAdd"]) {
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
else{
tv.usingText.text=self.text;
}
}
}
so instead of this
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
you should be doing this:
TextAddViewController *vc = [segue destinationViewController];
[vc setMyObjectHere:object];
Where in your case the object would be the string you want to set.
This way, you don't need you GetttingText IBAction.
I hope this helps you on your way. If you need some clarification feel free to ask. Another suggestion is to keep reading the Apple Documentation. It might not make sense at the moment, but eventually you will be able to find out stuff like this from the documentation

UITapGestureRecognizer is nil

I have a table where a gesturerecognizer should be added to the table-header-label. This is done through following code:
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTableheaderTap:)];
[headerView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;
The selector-method looks like following:
- (void)handleTableheaderTap:(UITapGestureRecognizer *)recognizer
Unfortunately, the recognizer is nil, when the method is called. What do I have to change, so the recognizer is in the handletTableheaderTap not nil anymore?
Thank you!
You have to store the instance in a controller attribute.
Declare a private property to store it.
Example :
#interface MyViewController()
#property (nonatomic, strong) UITapGestureRecognizer *singleFingerTap
#end
...
self.singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTableheaderTap:)];
[headerView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;

RotationGesture not call when PinchGesture active

I have drawingView and listen UIPanGestureRecognizer, UIRotationGestureRecognizer, and UIPinchGestureRecognizer on it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
[self.drawingView addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateRecognizer:)];
[self.drawingView addGestureRecognizer:rotateRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchRecognizer:)];
[self.drawingView addGestureRecognizer:pinchRecognizer];
[self.drawingView reloadData];
}
-(void) pinchRecognizer:(UIPinchGestureRecognizer*) recognizer {
return;
NSLog(#"Call scale");
}
- (void)rotateRecognizer:(UIRotationGestureRecognizer*)recognizer {
NSLog(#"Call rotaion");
}
If i only choose UIRotationGestureRecognizer or UIPinchGestureRecognizer it is perfect. But if using UIRotationGestureRecognizer and UIPinchGestureRecognizer only UIPinchGestureRecognizer called, UIRotationGestureRecognizer isn't called.
What is problem in my code?
I think i will make a UISegmented to choose mode , UIRotationGestureRecognizer or UIPinchGestureRecognizer, what should i do?
Thank a lot
If you want to have multiple gestures recognized at once, try using gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer, ex:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Edit: In addition to including the delegate in your .h, make sure to set your UIGestureRecognizer's delegate's to self, ex:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
panRecognizer.delegate = self;
[self.drawingView addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotateRecognizer:)];
rotateRecognizer.delegate = self;
[self.drawingView addGestureRecognizer:rotateRecognizer];
Use requireGestureRecognizerToFail: to recognize the gesture if the other gesture recognizer did not executes.
[rotateRecognizer requireGestureRecognizerToFail: pinchRecognizer];

Collision not working between items

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

Resources