#addGestureRecognizer
Explore tagged Tumblr posts
arthurknopper · 7 years ago
Text
Swipe Gesture iOS Tutorial
The iOS SDK can detect a number of gestures. In this tutorial we show how to detect the left and right swipe gesture. We will create a label which moves to the side of the swipe.This tutorial is made with Xcode 9 and built for iOS 11.
Open Xcode and create a new Single View App.
For product name, use IOS11SwipeGestureTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.
Go to the Storyboard and drag a Labelthe main view. Set the title of the Labe to ".Swipe" .Select the Button and select the Auto Layout align button. Select the "Horizontally in Container" checkbox and click "Add 1 Constraint".
Select the Button and select the Auto Layout pin button. Pin the Button to the top and and click "Add 1 Constraint".
The Storyboard should look like this.
Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Labe to the ViewController class and create the following Outlet
override func viewDidLoad() { super.viewDidLoad() let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))) let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))) leftSwipe.direction = .left rightSwipe.direction = .right view.addGestureRecognizer(leftSwipe) view.addGestureRecognizer(rightSwipe) }
The Swipe Gesture Recognisers are defined and added to the view. When a gesture is detected the HandleSwipes method is called, implement this method.
@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) { if (sender.direction == .left) { print("Swipe Left") let labelPosition = CGPoint(x: self.swipeLabel.frame.origin.x - 50.0, y: self.swipeLabel.frame.origin.y) swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: self.swipeLabel.frame.size.width, height: self.swipeLabel.frame.size.height) } if (sender.direction == .right) { print("Swipe Right") let labelPosition = CGPoint(x: self.swipeLabel.frame.origin.x + 50.0, y: self.swipeLabel.frame.origin.y) swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: self.swipeLabel.frame.size.width, height: self.swipeLabel.frame.size.height) } }
The direction of the swipe is checked, when a left swipe is detected the label moves left 50 points and with a right swipe it moves 50 points to the right. Build and Run the project and perform some swipes to move the label.
Build and Run the project.
You can download the source code of the IOS11SwipeGestureTutorial at the ioscreator repository on Github.
0 notes
ktrkathir · 9 years ago
Text
UISwipeGestureRecognizer in iOS
USE: UIGestureRecognizerDelegate Swipe gesture for left direction UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGestureLeft.numberOfTouchesRequired = 1; swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; swipeGestureLeft.delegate = self; [self.view…
View On WordPress
0 notes
jacob-cs · 7 years ago
Video
youtube
Swift: How to Chain Animations for Impressive Visual Effects
my review point is /10
https://youtu.be/xWNv-j75OQ8?t=5m34s   programmatically setting up auto layout (크기 설정하는 방법, 좌우 앵커 설정하는 방법, 글자 크기, 멀티라인 설정)
numberOfLines 프로퍼티 설정 (0)으로 멀티 라인 이 가능해 진다
https://youtu.be/xWNv-j75OQ8?t=10m26s   애니메이션 설명 (addGestureRecognizer)
https://youtu.be/xWNv-j75OQ8?t=11m5s   stack view에서 각 요소별 간격 조정 (UIStackview.spacing)
0 notes
arthurknopper · 8 years ago
Text
Snap Behaviour iOS Tutorial
As part of UIKit Dynamics you can add Snap Behavior to a view, which allows it  to be “snapped” to a specific location. The view will move to its new position as if it is pulled by a spring. In this tutorial we will let the user click on the screen to "snap" an image into place. This tutorial is made with Xcode 8.3.3 and built for iOS 10.3.
Open Xcode and create a new Single View Application.
For product name, use IOS10SnapBehaviourTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.
Go the Storyboard and drag an Image View to the main view, Go to the Size Inspector and set the size to 100 by 100 points.
We need a image to insert in our Image View. Download this image, unpack it and add it to the project. 
Select the Image View, go to the Attributes Inspector and add the apple.jpg file to Image field.
The Storyboard should now look like this.
Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Image View to the ViewController class and create the following Outlet.
Go to the ViewController.swift file and add the following properties
var animator:UIDynamicAnimator! var snapBehaviour:UISnapBehavior!
The animator property provides physics-related capabilities for its item and the snapBehaviour property will be applied to the Image View later on. Next, change the viewDidLoad method to
override func viewDidLoad() { super.viewDidLoad() // Create the Tap Gesture Recognizer let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userHasTapped)) self.view.addGestureRecognizer(tapGesture) // Create the Dynamic Animator animator = UIDynamicAnimator(referenceView: self.view) }
A Tap Gesture Recognizer is created, which will call the userHasTapped method when the user taps on the screen. The Dynamic Animator is created with the main view as its reference view. Next, implement the userHasTapped method
func userHasTapped(tap:UITapGestureRecognizer) { let touchPoint = tap.location(in: self.view) if snapBehaviour != nil { animator.removeBehavior(snapBehaviour) } snapBehaviour = UISnapBehavior(item: imageView, snapTo: touchPoint) snapBehaviour.damping = 0.3 animator.addBehavior(snapBehaviour) }
First the point is captured where the user has touched the screen. There can be only one Snap Behaviour instance active so we check if there is already a behaviour active. If so, we will remove this behaviour. Next the Snap Behaviour is initialized with the item:snapToPoint initializer. The item is our Image View and the point to snap to is the user's touch input. The damping property is the amount of oscillation the item has of the end of the snap animation. Finally, the Snap Behaviour is added to the Dynamic Animator. 
Build and Run the project, click anywhere on the screen to "snap" the image to its new location.
You can download the source code of the IOS10SnapBehaviourTutorial at the ioscreator repository on Github
0 notes