UIPanGestureRecognizer stays at state begin - ios

I have a SpringButton which I want to drag. This is my code:
var gesture = UIPanGestureRecognizer(target: self, action: #selector(userDragged))
card.addGestureRecognizer(gesture)
var cardIsCurrentlyMoving = false
func userDragged(gesture: UIPanGestureRecognizer){
if !cardIsCurrentlyMoving{
if let button = gesture.view as? SpringButton {
if gesture.state == .began {
print("begon")
cardIsCurrentlyMoving = true
startPosition = button.center
} else if gesture.state == .changed {
print("changed")
} else if gesture.state == .ended{
print("ended")
cardIsCurrentlyMoving = false
}
}
}
}
The function gets called, and the state gets changed to .began. However, when trying to move the button nothing happens. This is because cardIsCurrentlyMoving is set to true in .began, but never back to false, because gesture.state .changed and .ended never gets called.
When I release my finger and touch the button again, nothing happens as well. Why does UIPanGestureRecognizer not executes .changed and .ended?
Thanks.

I think you need to check your if statement
if !cardIsCurrentlyMoving{
}
Pan Gesture continuously calls method with changed state. So userDragged func called continuously but just because of your above if statement, control doesn't go inside if statement.
So i'll suggest to use this, instead of yours.
func userDragged(gesture: UIPanGestureRecognizer){
if let button = gesture.view as? SpringButton {
if gesture.state == .began {
print("begon")
cardIsCurrentlyMoving = true
startPosition = button.center
} else if gesture.state == .changed {
print("changed")
} else if gesture.state == .ended{
print("ended")
cardIsCurrentlyMoving = false
}
}
}

Related

UILongPressGestureRecognizer not recognizing .ended state

As the title reads, my problem is that my UILongPressGestureRecognizer sometimes does not run the code inside the sender.state = .ended. The .began always runs and works. I have tried to notice pattern but is to infrequent and I have not found a valid pattern or causation. I simply add my UITapGestureRecognizer UILongPressGestureRecognizer to my button:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.numberOfTapsRequired = 1
camButton.addGestureRecognizer(tapGesture)
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.10
camButton.addGestureRecognizer(longGesture)
And then here is my longTap function:
#objc func longTap(_ sender: UIGestureRecognizer) {
if sender.state == .ended {
if movieOutput.recordedDuration.seconds == lastRecordDuration || movieOutput.recordedDuration.seconds <= 0.35 {
capturePhoto()
} else {
stopRecording()
}
} else if sender.state == .began {
startCapture()
}
}
I use the longPress for video and photos, and the TapGesture for photos only. Im using AVFoundation.
After receiving assistance from #rmaddy, the solution is basically to implement a .cancelled state action. For some reason the continuous gesture of UILongPressGesture got cancelled. In my code I implemented a `if sender.state == .cancelled.
I know this is old question, but writing for those who are facing issue like me.
You can get sender.state == .possible when tap is ended.

Deselect UIView "button" when dragging finger off of it

I have a UIView that I added a UILongPressGestureRecognizer to so that I can handle clicks and have that UIView work like a button.
let longPressGtr = UILongPressGestureRecognizer(target: self, action:#selector(longPressSelector))
longPressGtr.minimumPressDuration = 0.1
myView.isUserInteractionEnabled = true
myView.addGestureRecognizer(longPressGtr)
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
myView.backgroundColor = UIColor.gray
} else if gestureRecognizer.state == .ended {
myView.backgroundColor = UIColor.blue // my button is blue
doSomething()
}
}
func doSomething() {
print("view was pressed")
}
This works, but the one thing that doesn't is when I press and hold on my UIView but drag my finger off the view, the "button" doesn't unselect. It still fires doSomething(). A regular UIButton will deselect the button and not fire it's onClick if you are holding down on it an drag your finger off the view.
How can I implement this functionality into my UIView?
Or is there a better way to make a UIView act like a button?
You need to check whether the gesture is inside the view.
#objc func longPresserDidFire(_ presser: UILongPressGestureRecognizer) {
let gestureIsInside = myView.point(inside: presser.location(in: myView), with: nil)
switch presser.state {
case .began, .changed:
if gestureIsInside {
myView.backgroundColor = .blue
} else {
myView.backgroundColor = .gray
}
case .cancelled:
myView.backgroundColor = .gray
case .ended:
myView.backgroundColor = .gray
if gestureIsInside {
doSomething()
}
default: break
}
}
You are not adding condition of gestureRecognizer state changed that's why it is accepting the end state.
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
} else if gestureRecognizer.state == .changed {
}else if gestureRecognizer.state == .ended {
}
}
Add one more condition and check if it works.
When you drag your finger outside of the view, gestureRecognizer is probably translated to failed or cancelled state, so you need to add handling of this cases gestureRecognizer.state == .failed and gestureRecognizer.state == .cancelled.
#objc func longPressSelector(_ gestureRecognizer: UILongPressGestureRecognizer) {
}
Place UILongPressGestureRecognizer instead of UITapGestureRecognizer and check it

How do I set it such that the pan recogniser increases a value

How do I set it such that the pan recogniser increases an Int variable when swiping right and then decreases the existing variable when sliding left.
#IBAction func customiseWhiteBalance(_ sender: UIPanGestureRecognizer) {
// Insert gesture value change here....
if sender.state == .began || sender.state == .changed {
let translation = sender.translation(in: self.view)
sender.setTranslation(CGPoint.zero, in: view)
print(translation)
}
}
Thanks
Pretty easy:
1: Add a counter:
var counter = 0
2: Declare your gestures and add them to your view (or where you need them)
let right = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe(_:)))
right.direction = .right
self.view.addGestureRecognizer(right)
let left = UISwipeGestureRecognizer(target: self, action: #selector(didViewSwipe(_:)))
left.direction = .left
self.view.addGestureRecognizer(left)
3: Get the gesture and start the counting
func didViewSwipe(_ gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
counter = counter + 1
case UISwipeGestureRecognizerDirection.left:
if counter > 0{
counter = counter - 1
}
default:
break
}
}
}

state began and ended not being triggered

I'm trying to make a button for Taking pictures and recording videos. When a long press is made it will record and 1 tap will take a picture. when button is being pressed i want to transform it to create a effect. however the began and ended is not being triggered since it is not transforming?
func centerButtonPressedDown(sender: UITapGestureRecognizer) {
if !pictureTaken {
delegate?.didLongTapCameraButton()
} else {
}
}
func centerButtonClicked(sender: UITapGestureRecognizer) {
if sender.state == .began {
self.centerButton.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
} else if sender.state == .ended {
self.centerButton.transform = CGAffineTransform.identity
}
}
CenterButton
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(centerButtonClicked)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(centerButtonPressedDown))
tapGesture.numberOfTapsRequired = 1
centerButton.addGestureRecognizer(tapGesture)
centerButton.addGestureRecognizer(longGesture)
I tried your code brother first.It shows me error.Then i modified the code it works fine.
let tapGesture = UITapGestureRecognizer(target: self, action:Selector("centerButtonClicked:")) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action:Selector("centerButtonPressedDown:"))
tapGesture.numberOfTapsRequired = 1
centerButton.addGestureRecognizer(tapGesture)
centerButton.addGestureRecognizer(longGesture)
func centerButtonPressedDown(sender: UILongPressGestureRecognizer) {
if sender.state == .Began {
print("long press began")
}
else if sender.state == .Ended {
print("long press Ended")
}
}
func centerButtonClicked(sender: UITapGestureRecognizer) {
print("tap is detected")
}
When I single tap the button the printed result is
tap is detected
When I long press the button the printed result is
long press began
long press Ended

How to programmatically send a pangesture in swift

I have a view that has panGesture functionality, but I need to send a pan-gesture from one point to another programatically. Is there a way to do this in swift using an animation with a specific time interval? Here is my attempt at calling the pan gesture programmatically:
var upPanPoint = CGPoint(x: contentView.center.x, y: contentView.center.y + 500)
var upPan = panGestureRecognizer.setTranslation(upPanPoint, inView: self)
onSwipe(upPan)
here is the code that recognizes the pan gesture:
func onSwipe(panGestureRecognizer : UIPanGestureRecognizer!) {
let view = panGestureRecognizer.view!
print(view)
switch (panGestureRecognizer.state) {
case UIGestureRecognizerState.Began:
if (panGestureRecognizer.locationInView(view).y < view.center.y) {
self.viewState.rotationDirection = .RotationAwayFromCenter
} else {
self.viewState.rotationDirection = .RotationTowardsCenter
}
case UIGestureRecognizerState.Ended:
self.finalizePosition()
default:
let translation : CGPoint = panGestureRecognizer.translationInView(view)
view.center = self.viewState.originalCenter + translation
self.rotateForTranslation(translation, withRotationDirection: self.viewState.rotationDirection)
self.executeOnPanForTranslation(translation)
}
}
// The Pan Gesture
func createPanGestureRecognizer(targetView: UIImageView) {
var panGesture = UIPanGestureRecognizer(target: self, action:("handlePanGesture:"))
targetView.addGestureRecognizer(panGesture)
}
func handlePanGesture(panGesture: UIPanGestureRecognizer) {
// get translation
var translation = panGesture.translationInView(view)
panGesture.setTranslation(CGPointZero, inView: view)
println(translation)
// create a new Label and give it the parameters of the old one
var label = panGesture.view as UIImageView
label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y)
label.multipleTouchEnabled = true
label.userInteractionEnabled = true
if panGesture.state == UIGestureRecognizer.State.began {
// add something you want to happen when the Label Panning has started
}
if panGesture.state == UIGestureRecognizer.State.ended {
// add something you want to happen when the Label Panning has ended
}
if panGesture.state == UIGestureRecognizer.State.changed {
// add something you want to happen when the Label Panning has been change ( during the moving/panning )
} else {
// or something when its not moving
}
}
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGesture))
self.imageView.addGestureRecognizer(panGesture)
#objc func panGesture(sender: UIPanGestureRecognizer){
let point = sender.location(in: view)
let panGesture = sender.view
panGesture?.center = point
print(point)
}
With Swift version 4.2 you can set pan gesture programmatically using below code:
let panGesture = UIPanGestureRecognizer(target: self, action:(#selector(self.handleGesture(_:))))
self.view.addGestureRecognizer(panGesture)
#objc func handleGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
case .changed:
case .cancelled:
case .ended:
default:
break
}
}

Resources