Created
May 20, 2012 09:23
-
-
Save blach/2757471 to your computer and use it in GitHub Desktop.
Code excerpt from Textastic's cursor navigation wheel. Shows how the move button is implemented.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) awakeFromNib { | |
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; | |
[buttonMove addGestureRecognizer:panGesture]; | |
[panGesture release]; | |
} | |
- (void) setupMoveTransform:(BOOL) move { | |
CATransform3D targetTransform = CATransform3DMakeScale(1.1f, 1.1f, 1.0f); | |
targetTransform = CATransform3DTranslate(targetTransform, -6, -6, 0.0f); | |
if (move) { | |
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; | |
animation.duration = 0.1f; | |
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; | |
animation.toValue = [NSValue valueWithCATransform3D:targetTransform]; | |
animation.removedOnCompletion = NO; | |
animation.fillMode = kCAFillModeForwards; | |
[self.layer addAnimation:animation forKey:@"moveTransform"]; | |
} else { | |
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; | |
animation.duration = 0.1f; | |
animation.fromValue = [NSValue valueWithCATransform3D:targetTransform]; | |
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; | |
animation.removedOnCompletion = YES; | |
[self.layer addAnimation:animation forKey:@"moveTransform"]; | |
} | |
} | |
- (IBAction) touchDownButton:(UIButton *) sender { | |
if (sender == buttonMove) { | |
[self setupMoveTransform:YES]; | |
} | |
} | |
- (void) panGesture:(UIPanGestureRecognizer *) r { | |
UIGestureRecognizerState st = r.state; | |
CGPoint delta = [r translationInView:self.superview]; | |
if (st == UIGestureRecognizerStateBegan) { | |
buttonMove.selected = YES; | |
} | |
// UIPanGestureRecognizer will return a delta of { -NAN, -NAN } sometimes (if it would be outside the parent view's bounds maybe?). | |
if ((isfinite(delta.x) && isfinite(delta.y)) && (st != UIGestureRecognizerStateBegan || !(delta.x == 0 && delta.y == 0))) { | |
CGRect frame = self.frame; | |
frame.origin.x += delta.x; | |
frame.origin.y += delta.y; | |
[r setTranslation:CGPointZero inView:self.superview]; | |
self.frame = frame; | |
} | |
if (st == UIGestureRecognizerStateEnded) { | |
[self setupMoveTransform:NO]; | |
CGRect bounds = editorView.scrollView.superview.bounds; | |
CGPoint position = [self.superview convertPoint:self.center toView:editorView.scrollView.superview]; | |
CGRect frame = self.frame; | |
if (position.x + frame.size.width / 2 > bounds.size.width) { | |
position.x = bounds.size.width - frame.size.width / 2; | |
} | |
if (position.y + frame.size.height / 2 > bounds.size.height) { | |
position.y = bounds.size.height - frame.size.height / 2; | |
} | |
if (position.y - frame.size.height / 2 < 0) { | |
position.y = frame.size.height / 2; | |
} | |
if (position.x - frame.size.width / 2 < 0) { | |
position.x = frame.size.width / 2; | |
} | |
position = [editorView.scrollView.superview convertPoint:position toView:self.superview]; | |
frame.origin.x = (int)(position.x - frame.size.width / 2); | |
frame.origin.y = (int)(position.y - frame.size.height / 2); | |
[UIView beginAnimations:nil context:nil]; | |
self.frame = frame; | |
[UIView commitAnimations]; | |
buttonMove.selected = NO; | |
[editorView saveNavigationWheelPosition]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment