In my iPad app, I want the users to be able to resize a UIView
by dragging the view from its edges. I'll be using iOS 5 SDK, so what's the cleanest approach to do this? Are there any alternatives to achieving this without dealing with touchesBegan, touchesMoved,... etc?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
This is a Swift 4.2 Solution which works with AutoLayout & Constraint-Animation.
Since it is recommended to animate the constraints rather than the actual size of a rectangle when working with AutoLayout, this solutions does exactly that.
Additional features:
Checkout a video of it here: https://imgur.com/a/CrkARLi
Import is to connect the constraints as outlets, to animate them.
I put this as working project on Github too: https://github.com/ppoh71/resizeRectangleOnTouchDrag
I'm guessing your UI involves some kind of handles on the sides of the view, and attaching a simple
UIPanGestureRecognizer
to those handle controls makes the whole problem pretty easy.In the action method from the gesture recognizer, just get the
-translationInView:
relative to the view you're resizing, save off the original frame when the gesture recognizer's state isUIGestureRecognizerStateBegan
, and adjust the view's frame continually while the state isUIGestureRecognizerStateChanged
.You can do this by checking the touch-start point. If it hits one of your four corners you can resize based on the distance between that touch-start point and the current-touch point. (If the touch-start point didn't hit a corner, we just move the view instead of resizing.)
Define the size of your draggable corners.
Add these instance variables to your class to keep track of touch state and which way we're resizing.
Handle the touch start / change events.
Swift Version of @Prerna Chavan solution , Prerna solution does not detect if user touch on edges, it is detecting only corners, however below code detects all
I updated above code using
enum
.currentEdge
saves state of touch position of user.