Skip to content

Instantly share code, notes, and snippets.

@alistairholt
Last active December 19, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairholt/5875396 to your computer and use it in GitHub Desktop.
Save alistairholt/5875396 to your computer and use it in GitHub Desktop.
Dragging a UIView
# 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