Moving view along with fingure touch

M

You can move any view inside your viewcontroller simply adding pangestureRecogniser

-(void)ViewDidLoad
{
//add pangesture recognise to your moving view
UIPanGestureRecognizer *mSwipeUpRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handletapgesture:)];
[mSwipeUpRecognizer setMaximumNumberOfTouches:1];
[mSwipeUpRecognizer setMaximumNumberOfTouches:1];

[Yourview addGestureRecognizer:mSwipeUpRecognizer];

}
-(void)handletapgesture:(id )recognizer
{
UIPanGestureRecognizer *gesture=(UIPanGestureRecognizer*)recognizer;

[self.view bringSubviewToFront:gesture.view];

switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
//do something when touch identified
NSLog(@”Received a pan gesture”);
panCoord = [gesture locationInView:gesture.view];

}
break;

case UIGestureRecognizerStateChanged:
{
//change the view frame
CGPoint newCoord = [gesture locationInView:gesture.view];
float dX = newCoord.x-panCoord.x;
float dY = newCoord.y-panCoord.y;

gesture.view.frame = CGRectMake(0, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height);
}
break;
case UIGestureRecognizerStateEnded:
{
if (gesture.view.frame.origin.y<=originalheight && directionUp==0)
{
//if Uiview position down move it to Up
[UIView animateWithDuration: 0.4
delay: 0
options: (UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveEaseOut)
animations:^{
UpDownview.frame=CGRectMake(0,newheight, 320,325);

}
completion:^(BOOL finished) {
directionUp=1;

}];

}
else
{
//if Uiview position up move it to down
[UIView animateWithDuration: 0.4
delay: 0
options: (UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveEaseOut)
animations:^{
UpDownview.frame=CGRectMake(0, originalheight, 320, 200);

}
completion:^(BOOL finished) {
directionUp=0;
// return;

}];
}
}

break;
default:
break;
}

}

About the author

sanjay.raskar
By sanjay.raskar

Category