Last active
December 19, 2015 01:19
-
-
Save alistairholt/5875396 to your computer and use it in GitHub Desktop.
Dragging a UIView
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
# Example: UIPanGestureRecognizer on a single UIView | |
class FooViewController < UIViewController | |
def loadView | |
# View | |
self.view = UIView.alloc.init | |
# Block | |
blockFrame = CGRectMake(100,100,100,100) | |
block = UIView.alloc.initWithFrame(blockFrame) | |
block.backgroundColor = UIColor.redColor | |
view.addSubview(@block) | |
# Pan gesture recognizer | |
gestureRecognizer = UIPanGestureRecognizer.alloc.initWithTarget(self, action: 'dragBlock:') | |
gestureRecognizer.maximumNumberOfTouches = 1 | |
block.addGestureRecognizer(gestureRecognizer) | |
end | |
def dragBlock(gestureRecognizer) | |
targetView = gestureRecognizer.view | |
translatedPoint = gestureRecognizer.translationInView(targetView) | |
movedPoint = CGPointMake(targetView.center.x + translatedPoint.x, targetView.center.y + translatedPoint.y) | |
targetView.center = movedPoint | |
gestureRecognizer.setTranslation(CGPointZero, inView: targetView) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment