I have 5 buttons; each allows touchUpInside-action and touchDragOutside-action...as well as doubleTap-action and longPress-action via tapGestureRecognizers.
I would also like to allow user to start a swipe from any UIButton and Any addition UIButton that the swipe touches, these buttons (including first touch) perform their #IBAction func Swipe.
so a continuously swipe like so
would perform #IBAction func swipe for UIButtons 1,2,3,4 and 5.
You can try something like this:
// Create an array to hold the buttons you've swiped over
var buttonArray:NSMutableArray!
override func viewDidLoad() {
super.viewDidLoad()
// Make your view's UIPanGestureRecognizer call panGestureMethod:
// (don't use a UISwipeGestureRecognizer since it's a discrete gesture)
panGesture.addTarget(self, action: "panGestureMethod:")
}
func panGestureMethod(gesture:UIPanGestureRecognizer) {
// Initialize and empty array to hold the buttons at the
// start of the gesture
if gesture.state == UIGestureRecognizerState.Began {
buttonArray = NSMutableArray()
}
// Get the gesture's point location within its view
// (This answer assumes the gesture and the buttons are
// within the same view, ex. the gesture is attached to
// the view controller's superview and the buttons are within
// that same superview.)
let pointInView = gesture.locationInView(gesture.view)
// For each button, if the gesture is within the button and
// the button hasn't yet been added to the array, add it to the
// array. (This example uses 4 buttons instead of 9 for simplicity's
// sake
if !buttonArray.containsObject(button1) && CGRectContainsPoint(button1.frame, pointInView){
buttonArray.addObject(button1)
}
else if !buttonArray.containsObject(button2) && CGRectContainsPoint(button2.frame, pointInView){
buttonArray.addObject(button2)
}
else if !buttonArray.containsObject(button3) && CGRectContainsPoint(button3.frame, pointInView){
buttonArray.addObject(button3)
}
else if !buttonArray.containsObject(button4) && CGRectContainsPoint(button4.frame, pointInView){
buttonArray.addObject(button4)
}
// Once the gesture ends, trigger the buttons within the
// array using whatever control event would otherwise trigger
// the button's method.
if gesture.state == UIGestureRecognizerState.Ended && buttonArray.count > 0 {
for button in buttonArray {
(button as UIButton).sendActionsForControlEvents(UIControlEvents.TouchUpInside)
}
}
}
(Edit: Here are a few answers I've written in the past explaining what I meant by UISwipeGestureRecognizer being a discrete gesture: stackoverflow.com/a/27072281/2274694, stackoverflow.com/a/25253902/2274694)
Related
I have a project where I’m adding three UILabels to the view controller’s view. When the user begins moving their finger around the screen, I want to be able to determine when they their finger is moving over any of these UILabels.
I’m assuming a UIPanGestureRecognizer is what I need (for when the user is moving their finger around the screen) but I’m not sure where to add the gesture. (I can add a tap gesture to a UILabel, but this isn’t what I need)
Assuming I add the UIPanGestureRecognizer to the main view, how would I go about accomplishing this?
if gesture.state == .changed {
// if finger moving over UILabelA…
// …do this
// else if finger moving over UILabelB…
// …do something else
}
You can do this with either a UIPanGestureRecognizer or by implementing touchesMoved(...) - which to use depends on what else you might be doing.
For pan gesture, add the recognizer to the view (NOT to the labels):
#objc func handlePan(_ g: UIPanGestureRecognizer) {
if g.state == .changed {
// get the location of the gesture
let loc = g.location(in: view)
// loop through each label to see if its frame contains the gesture point
theLabels.forEach { v in
if v.frame.contains(loc) {
print("Pan Gesture - we're panning over label:", v.text)
}
}
}
}
For using touches, no need to add a gesture recognizer:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let t = touches.first {
// get the location of the touch
let loc = t.location(in: view)
// loop through each label to see if its frame contains the touch point
theLabels.forEach { v in
if v.frame.contains(loc) {
print("Touch - we're dragging the touch over label:", v.text)
}
}
}
}
I am looking for a simple way to detect when the user starts to touch the screen regardless of whether they are touching on a UIButton or elsewhere on the screen.
If I use touchesBegan(... on my ViewController, it does not detect touches on controls like UIButtons.
There is UITapGesturReconizer on ViewController but that would fire only when the tap has completed. I am looking to detect when any touch begins.
Use a UILongPressGestureRecognizer and set its minimumPressDuration to 0. It will act like a touch down during the UIGestureRecognizerStateBegan state.
func setupTap() {
let touchDown = UILongPressGestureRecognizer(target:self, action: #selector(didTouchDown))
touchDown.minimumPressDuration = 0
view.addGestureRecognizer(touchDown)
}
#objc func didTouchDown(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
doSomething()
}
}
As a user, I want to play the game Rock, Paper, Scissors. On the view controller, I should see images for the rock, paper, and scissors. Once I tap an image to select my game piece, I want the app to randomly generate a game piece. Based on what I picked and what the app picked, I want a label to display who is the winner.
No clue where to start
needs to use tap gestures recognizer
Add Tag value to the imageView and on tap function use that to show the respective image.
override func viewDidLoad() {
super.viewDidLoad()
self.myUIImageView1.tag = 100 // replace with your imageviews and desired tag id
self.myUIImageView2.tag = 200 // replace with your imageviews and desired tag id
self.myUIImageView3.tag = 300 // replace with your imageviews and desired tag id
// Add `UITapGestureRecognizer` to all ImageViews, I've added for `myUIImageView1`
let imageViewTap = UITapGestureRecognizer(target: self, action: #selector(self.myUIImageViewTap(_:)))
imageViewTap.numberOfTapsRequired = 1
imageViewTap.numberOfTouchesRequired = 1
self.myUIImageView1.addGestureRecognizer(imageViewTap)
self.myUIImageView1.userInteractionEnabled = true
}
#objc func myUIImageViewTap(_ sender: UITapGestureRecognizer) {
// Based on the TAG Value you can redirect the user to particular image section.
if let view = sender.view {
if view.tag == 100 {
//Image1 tapped
} else if view.tag == 200 {
//Image2 tapped
} else if view.tag == 300 {
//Image3 tapped
}
}
}
I'm not sure whether this has been asked or not, but I failed to find a solution. I'm implementing panning gesture on a button, but the idea is: the button is fixed to a position, and when the user drags it, a copy of the button is created and moving with the gesture; the original one stays at its initial place (so there'll be 2 buttons in the view). When the panning ends, the new button is used for some processing, and after that it should disappear (the original one stays as it is; so this whole process can repeat). Currently what I have is as below:
private func addPanGesture() {
for btn in self.selectors { //selectors is a list of buttons which needs this gesture
let pan = UIPanGestureRecognizer(target: self, action:#selector(self.panDetected(_:)))
pan.minimumNumberOfTouches = 1
pan.maximumNumberOfTouches = 1
btn.addGesturerecognizer(pan)
}
}
#objc private func panDetected(_ panGesture: UIPanGestureRecognizer) {
var translation = panGesture.translation(in: view)
panGesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
var newButton = UIButton()
if let initButton = panGesture.view as? UIButton {
print ("Button recognized!") // this msg is printed out
newButton.center = CGPoint(x: initButton.center.x + translation.x, y: initButton.center.y + translation.y)
newButton.setImage(UIImage(named: "somename"), for: .normal)
}
if panGesture.state == UIGestureRecognizerState.began {
self.view.addSubview(newButton)
}
if panGesture.state == UIGestureRecognizerState.ended {
//some other processing
}
if panGesture.state == UIGestureRecognizerState.changed {
self.view.addSubview(newButton)
}
// printed-out msgs show began, ended, changed states have all been reached
}
But the new button doesn't show up in my view. May I know how to solve this?
You need to create and add the new button as a subview only on .began and remove it on .ended.
Therefore you need to keep a reference to the new button.
You are setting the new button's center but not it's size. You might set its .frame.
You do not need to set a translation to the pan gesture. When you get var translation = panGesture.translation(in: view) you get everything you need.
I have wrote the below code for only one button, but if you are going to allow simultaneous dragging of buttons, you would need to keep a list of moving buttons instead of var movingButton: UIButton?
private func addPanGesture() {
let pan = UIPanGestureRecognizer(target: self, action:#selector(self.panDetected(_:)))
pan.minimumNumberOfTouches = 1
pan.maximumNumberOfTouches = 1
btn.addGestureRecognizer(pan)
}
#objc private func panDetected(_ panGesture: UIPanGestureRecognizer) {
let translation = panGesture.translation(in: view)
let initButton = panGesture.view as! UIButton
if panGesture.state == UIGestureRecognizerState.began {
// this is just copying initial button
// this might be overkill
// just make sure you set the frame, title and image of the new button correctly
let initButtonData = NSKeyedArchiver.archivedData(withRootObject: initButton)
let newButton = NSKeyedUnarchiver.unarchiveObject(with: initButtonData) as! UIButton
// we store new button's reference since we will just move it while it is added to view
movingButton = newButton
self.view.addSubview(movingButton!)
}
if panGesture.state == UIGestureRecognizerState.ended {
//some other processing
// when we are done just we just remove it from superview
movingButton!.removeFromSuperview()
}
if panGesture.state == UIGestureRecognizerState.changed {
// at any change, all we need to do is update movingButton's frame
var buttonFrame = initButton.frame;
buttonFrame.origin = CGPoint(x: buttonFrame.origin.x + translation.x, y: buttonFrame.origin.y + translation.y)
movingButton!.frame = buttonFrame
}
}
Hard to say without debugging it, but a few things I see:
You create a new button every time through panDetected, and add it to the view each time. You should only create an add the button in the .began state.
You should use init(frame:) to create your button, and initialize it to the size of the image.
It looks like you're attaching the pan gestures to the buttons. Then you get the pan coordinates in the button's coordinate system, which doesn't make sense. You should be converting the pan gesture to the button's superview's coordinate system, and should not be calling setTranslation except when the pan gesture's state is .began.
You should be setting the button's coordinates to the new location of the pan gesture each time you get a 1st.changed` message.
this is my first question! I was just wondering, in Swift (specifically Swift 2, although that may go without saying!), how you create a button that the user can drag around. So for example, if it is a UIButton, the user can tap and hold it, and when they move their finger, the UIButton moves with it, and when they release it, it remains in the position that the user left it. Potentially there could be a snapping system but this is unimportant for now.
I've searched StackOverflow and found some quite interesting things, however it's all for Objective-C, and although Swift is pretty similar in some respects, I can't figure out in the slightest as to how to implement this in Swift.
It would be massively appreciated for a project that I am working on!
Thank you very much!
You can implement UIPanGestureRecognizer on your UIButton.
Wherever you create your button (viewDidLoad if using outlets):
let pan = UIPanGestureRecognizer(target: self, action: "panButton:")
button.addGestureRecognizer(pan)
This creates a new pan gesture recognizer and adds it to the button. Now, you'll want to implement the pan's action. First, you need to store the center of the button to be able to reset it when you finish panning. Add this as a view controller property:
var buttonCenter = CGPointZero
Then you implement the pan action. Note that you can use gesture recognizer states to determine when the pan starts and ends:
func panButton(pan: UIPanGestureRecognizer) {
if pan.state == .Began {
buttonCenter = button.center // store old button center
} else if pan.state == .Ended || pan.state == .Failed || pan.state == .Cancelled {
button.center = buttonCenter // restore button center
} else {
let location = pan.locationInView(view) // get pan location
button.center = location // set button to where finger is
}
}
Swift 4 & 5 Version of accepted answer:
var buttonCenter: CGPoint = .zero
viewDidLoad() {
super.viewDidLoad()
let pan = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.panButton(pan:)))
button.addGestureRecognizer(pan)
}
#objc func panButton(pan: UIPanGestureRecognizer) {
if pan.state == .began {
buttonCenter = button.center // store old button center
} else if pan.state == .ended || pan.state == .failed || pan.state == .cancelled {
button.center = buttonCenter // restore button center
} else {
let location = pan.location(in: view) // get pan location
button.center = location // set button to where finger is
}
}
Basically, you want to implement a touch gesture recognizer and set the button's center to the center of your press when you tap/move said button.
Here's how you'll want to do that: https://stackoverflow.com/a/31487087/5700898
Also, really cool idea!