I have the following code and no error when I run this. long press is working fine and double tap is not working. I have disabled the zoom before adding the double tap gesture.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
routeMapView.zoomEnabled = false
routeMapView.showsPointsOfInterest = true
let doubleTapGesture = UITapGestureRecognizer(target: self, action: "routeMapDoubleTapSelector:")
doubleTapGesture.numberOfTapsRequired = 2
routeMapView.addGestureRecognizer(doubleTapGesture)
let ulpgr = UILongPressGestureRecognizer(target: self, action:"routeMapLongPressSelector:")
ulpgr.minimumPressDuration = 2.0
routeMapView.addGestureRecognizer(ulpgr)
}
Any help?
I tried your code and it seem to be working fine. "double taps" is printed. Here's the test code.
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
let routeMapView = MKMapView()
self.view = routeMapView
routeMapView.zoomEnabled = false
routeMapView.showsPointsOfInterest = true
let doubleTapGesture = UITapGestureRecognizer(target: self, action: "routeMapDoubleTapSelector:")
doubleTapGesture.numberOfTapsRequired = 2
routeMapView.addGestureRecognizer(doubleTapGesture)
let ulpgr = UILongPressGestureRecognizer(target: self, action:"routeMapLongPressSelector:")
ulpgr.minimumPressDuration = 2.0
routeMapView.addGestureRecognizer(ulpgr)
}
func routeMapDoubleTapSelector(sender: AnyObject) {
NSLog("double taps")
}
func routeMapLongPressSelector(sender: AnyObject) {
NSLog("long press")
}
}
Related
I want to use double click. I have written function doubleTap. How to recognize location of finger?
override func viewDidLoad()
{
super.viewDidLoad()
let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTap")
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTap()
{
}
You can use location(ofTouch:in:) to get the location of the touch. However, you need access to the gesture recognizer from inside the function where you want to access the location, so you should declare doubleTap as an instance property of your class.
class YourViewController: UIViewController {
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(YourViewController.doubleTap))
override func viewDidLoad(){
super.viewDidLoad()
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTap(){
let touchLocation = doubleTap.location(ofTouch: numberOfTouches-1,in: nil)
}
}
Change the input parameters to the function if you want to change whether you need to get the first or last touch's location or if you want to get the location relative to a subview.
You can get the gesture recognizer as a parameter
override func viewDidLoad()
{
super.viewDidLoad()
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapFunc))
doubleTap.numberOfTapsRequired = 2
doubleTap.numberOfTouchesRequired = 1
view.addGestureRecognizer(doubleTap)
}
func doubleTapFunc(_ sender: UITapGestureRecognizer)
{
// Use 'sender' here to get the location
if sender.state == .ended {
// handling code
let location = sender.location(ofTouch: 0, in: self.view!)
}
}
In map view, I want to make that if user touches anywhere in the map, app stops updating location..but seems like nothing happens and it takes around 10-15 seconds for actions to work in the app ( makes app really slow and laggy ) I have been using this code:
#IBOutlet var Map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
StopUpdate.hidden = true
Longi.hidden = true
Lati.hidden = true
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
locationManager.startUpdatingLocation()
locationManager.stopUpdatingLocation()
self.view.addGestureRecognizer(UIGestureRecognizer(target: self, action: "tapClose"))
}
func tapClose(gesture: UITapGestureRecognizer){
locationManager.stopUpdatingLocation()
StopUpdate.hidden = true
UpdateLocation.hidden = false
}
Because of the GestureRecognizer my app is slow and laggy.Any solution to this?
Right way to add UITapGesture
class DashVC: UIViewController, UIGestureRecognizerDelegate{
}
define tapgesture in viewdidload like this
let tapDashBoard = UITapGestureRecognizer(target: self, action: #selector(DashVC.DashBoardTapped(_:)))
tapDashBoard.delegate = self
view.addGestureRecognizer(tapDashBoard)
and action
func DashBoardTapped(sender: UITapGestureRecognizer? = nil) {
}
Tap gesture event not gettint called. I may be doing something wrong, but please have a look (I tried adding recognizer to self.view but still no luck) :
LoginViewController
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let container = UIView()
container.heightAnchor.constraintEqualToConstant(50).active = true
let myVC1 = MyViewController()
let myVC2 = MyViewController()
let myVC3 = MyViewController()
let myVC4 = MyViewController()
let myStackView = UIStackView(arrangedSubviews: [myVC1.view, myVC2.view, myVC3.view, myVC4.view])
myStackView.spacing = 10
myStackView.alignment = .Fill
myStackView.distribution = .EqualSpacing
container.addSubview(myStackView)
view.addSubview(container)
}
}
MyViewController
class MyViewController: UIViewController, UIGestureRecognizerDelegate {
let ImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
ImageView.backgroundColor = UIColor.blueColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(pressedSocialMediaItem(_:)))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
ImageView.userInteractionEnabled = true
ImageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(ImageView)
}
func pressedSocialMediaItem(sender : UITapGestureRecognizer) {
print("PRESSED ! ")
}
}
I suggest using MyViewController.pressedSocialMediaItem, so your code should work like this.
class MyViewController: UIViewController, UIGestureRecognizerDelegate {
let ImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
ImageView.backgroundColor = UIColor.blueColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyViewController.pressedSocialMediaItem(_:)))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
ImageView.userInteractionEnabled = true
ImageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(ImageView)
}
func pressedSocialMediaItem(sender : UITapGestureRecognizer) {
print("PRESSED ! ")
}
}
I have tested the following on my machine and it works:
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.backgroundColor = UIColor.blueColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.pressedSocialMediaItem(_:)))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
imageView.userInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(imageView)
}
func pressedSocialMediaItem(sender : UITapGestureRecognizer) {
print("PRESSED ! ")
}
}
Okey, so I got it working somehow. All I need to do was to add view controllers inside my LoginViewController like so:
self.addChildViewController(myVC1)
Instead of using view.addSubview(container)
Notice: I'm not sure it is the correct way of doing this, and please comment if it's not. Meanwhile it solves my problem.
FYI, I am using this Github Player: https://github.com/piemonte/Player
I am trying to play a video, but it wont work. On the View Controller's UIView i use background color blue, and when I run this project, I only see a bue background without a player. This is my code:
import UIKit
import Player
import AVFoundation
class ViewController: UIViewController, PlayerDelegate {
#IBOutlet var myView: UIView!
private var player: Player!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")
self.player = Player()
self.player.delegate = self
player.view.frame.size.width = UIScreen.mainScreen().bounds.size.width
player.view.frame.size.height = player.view.frame.size.width
player.fillMode = AVLayerVideoGravityResizeAspectFill
player.view.sizeToFit()
myView.addSubview(player.view)
let videoUrl: NSURL = fileUrl!
self.player.setUrl(videoUrl)
self.player.playFromBeginning()
self.player.playbackLoops = true
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: PlayerDelegate Start
func handleTapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer) {
switch (self.player.playbackState.rawValue) {
case PlaybackState.Stopped.rawValue:
self.player.playFromBeginning()
case PlaybackState.Paused.rawValue:
self.player.playFromCurrentTime()
case PlaybackState.Playing.rawValue:
self.player.pause()
case PlaybackState.Failed.rawValue:
self.player.pause()
default:
self.player.pause()
}
}
func playerReady(player: Player) {
}
func playerPlaybackStateDidChange(player: Player) {
}
func playerBufferingStateDidChange(player: Player) {
}
func playerPlaybackWillStartFromBeginning(player: Player) {
}
func playerPlaybackDidEnd(player: Player) {
}
/* override func awakeFromNib() {
self.collectionView.autoresizesSubviews = true
self.collectionView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
self.collectionView.translatesAutoresizingMaskIntoConstraints = true
}*/
// MARK: PlayerDelegate Stop
}
Attempt nr 2:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")
self.player = Player()
self.player.delegate = self
self.player.view.frame = self.view.bounds
self.addChildViewController(self.player)
self.view.addSubview(self.player.view)
self.player.didMoveToParentViewController(self)
let videoUrl: NSURL = fileUrl!
self.player.setUrl(videoUrl)
self.player.playFromBeginning()
self.player.playbackLoops = true
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
When I try to open the link in Safari, it doesn't work, but in Chrome it works fine.. Any suggestions what might be wrong?
Actually the player is not actually added to your view yet.
Please follow this to add this on your view as a sub-viewcontroller.
self.player = Player()
self.player.delegate = self
self.player.view.frame = self.view.bounds
self.addChildViewController(self.player)
self.view.addSubview(self.player.view)
self.player.didMoveToParentViewController(self)
I have a issue with my swipe gesture on a MapView, to be more accurate I have issues with the first swipe gesture on a MapView because it does not does a function & moves the screen at the same time.
The code recognize my location, put an annotation on me, and runs a 5 sec timer that allows the app to add new annotations only every 5 secs. The MapView follows my path as usually. All good so far. Check the pic of my simulator at the end.
Then it occurred to me that it would be a good idea to allow the user to scape from that region and explore the rest of the map. The user can do that only swiping in any direction. And when the user is at any other location he can press the "getBack" button to center the map on his current location. All works good too.
The issue is that when the gesture Swipe is detected , that first swipe does not move the screen. It is only the second swift which does it.
I think that may be the first swipe only stops the map from following my path, and then the second one is the one that moves the screen.
Is there a way for the first swipe to stop following my path and move the map at the same time ?
I hope I made myself clear :(
The Code of the ViewController:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var map: MKMapView!
var locationManager = CLLocationManager()
let latDelta:CLLocationDegrees = 0.05
let longDelta:CLLocationDegrees = 0.05
var reStartCountClock = true
var getBackInPlace = true
var timer = NSTimer()
#IBAction func getBack(sender: AnyObject) {
getBackInPlace = true
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let uilpgr = UILongPressGestureRecognizer(target: self, action: "actionWhenPressed:")
uilpgr.minimumPressDuration = 1
map.addGestureRecognizer(uilpgr)
let uilpgrD = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
uilpgrD.direction = .Down
let uilpgrU = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
uilpgrU.direction = .Up
let uilpgrR = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
uilpgrR.direction = .Right
let uilpgrL = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
uilpgrL.direction = .Left
map.addGestureRecognizer(uilpgrD)
map.addGestureRecognizer(uilpgrR)
map.addGestureRecognizer(uilpgrL)
map.addGestureRecognizer(uilpgrU)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locationOnline:CLLocation = locations[0]
let spanOnline:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
let centerOnline:CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationOnline.coordinate.latitude, locationOnline.coordinate.longitude)
let regionOnline:MKCoordinateRegion = MKCoordinateRegionMake(centerOnline, spanOnline)
if getBackInPlace == true {
self.map.setRegion(regionOnline, animated: true)
}
if reStartCountClock == true {
timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "Anota", userInfo: nil, repeats: true)
reStartCountClock = false
let annotationOnline = MKPointAnnotation()
annotationOnline.title = "Updated Location"
annotationOnline.subtitle = "Pin Pam Pum"
annotationOnline.coordinate = centerOnline
self.map.addAnnotation(annotationOnline)
}
}
func handleSwipe (gestureRecognizer: UIGestureRecognizer)
{
getBackInPlace = false
print("Entra en el gesto")
}
func Anota (){
reStartCountClock = true
timer.invalidate()
}
func actionWhenPressed (gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.Began
{
getBackInPlace = false
print("Gesture Recognized")
let touch = gestureRecognizer.locationInView(self.map)
let touchCoordinate:CLLocationCoordinate2D = map.convertPoint(touch, toCoordinateFromView: self.map)
let touchAnnotation = MKPointAnnotation()
touchAnnotation.coordinate = touchCoordinate
touchAnnotation.title = "You tapped here"
touchAnnotation.subtitle = "It is a recommended place"
self.map.addAnnotation(touchAnnotation)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The Simulator: