Skip to content

Instantly share code, notes, and snippets.

@H3xept H3xept/Draggable UIView
Last active Aug 29, 2015

Embed
What would you like to do?
I made this trying to recreate iOS assistive touch. It's pretty basic, i have added snap to borders and show/hide animation. You can find an implementation in my repository "Tweak".
@interface FloatingView : UIView
-(void)dragged:(UIPanGestureRecognizer *)sender;
-(void)snapToLeft;
-(void)snapToRight;
-(void)snapToBottom;
-(void)snapToTop;
-(void)collapse;
-(void)show;
@end
@implementation FloatingView
-(instancetype)init{
self = [super initWithFrame:CGRectMake(ScreenWidth,270,55,55)];
return self;
self.backgroundColor = [UIColor redColor];
self.userInteractionEnabled = YES;
//DRAG
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragged:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
[panRecognizer release];
/* // ------ UNUSED IN THIS FILE, CHECK IMPLEMENTATION IN REPOSITORY "TWEAK"
//DOUBLE TAP
UITapGestureRecognizer* doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(switchSocial:)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:doubleTapRecognizer];
[doubleTapRecognizer release];
//SINGLE TAP
UITapGestureRecognizer* singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showSocialModel:)];
[singleTapRecognizer setNumberOfTapsRequired:1];
[self addGestureRecognizer:singleTapRecognizer];
[singleTapRecognizer release];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
*/ // ------ UNUSED IN THIS FILE, CHECK IMPLEMENTATION IN REPOSITORY "TWEAK"
return self;
}
-(void)dragged:(UIPanGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded){
if(sender.view.center.y > [[UIScreen mainScreen] bounds].size.height-100){
[self snapToBottom];
}
else if(sender.view.center.y < 100){
[self snapToTop];
}
else{
if(sender.view.center.x < [[UIScreen mainScreen] bounds].size.width/2){
[self snapToLeft];
}
else if(sender.view.center.x >= [[UIScreen mainScreen] bounds].size.width/2){
[self snapToRight];
}
}}
if (sender.state == UIGestureRecognizerStateChanged){
CGPoint translation = [sender translationInView:sender.view];
sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);
[sender setTranslation:CGPointMake(0, 0) inView:sender.view];
}
}
-(void)snapToLeft{
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseIn animations:^{
assistantView.frame = CGRectMake(5,assistantView.frame.origin.y,[assistantView bounds].size.width,[assistantView bounds].size.height);
} completion:^(BOOL finished){}];
}
-(void)snapToRight{
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseIn animations:^{
assistantView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width-[assistantView bounds].size.width-5,assistantView.frame.origin.y,[assistantView bounds].size.width,[assistantView bounds].size.height);
} completion:^(BOOL finished){}];
}
-(void)snapToBottom{
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseIn animations:^{
assistantView.frame = CGRectMake(assistantView.frame.origin.x,[[UIScreen mainScreen] bounds].size.height-[assistantView bounds].size.height-5,[assistantView bounds].size.width,[assistantView bounds].size.height);
} completion:^(BOOL finished){}];
}
-(void)snapToTop{
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseIn animations:^{
assistantView.frame = CGRectMake(assistantView.frame.origin.x,5,[assistantView bounds].size.width,[assistantView bounds].size.height);
} completion:^(BOOL finished){}];
}
-(void)collapse{
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.transform = CGAffineTransformMakeScale(0.01, 0.01);
} completion:^(BOOL finished){self.hidden = YES;}];
}
-(void)show{
self.transform = CGAffineTransformMakeScale(0.01, 0.01);
self.hidden = NO;
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.