Swipe Gesture in Swift 3

Custom Swipe gesture from one point with (x,y) to another point with (x,y) in swift 3

One way you could do this is by adding a transparent view to the area of the screen that you want a gesture to be recognized in. So, if we consider your case, you could add a view that is presented over the center of its superview to some point near the right of its superview. Set the isOpaque property to false. Add a swipe gesture to this view with the target being the superview, and the action being the method of the superview that you want to execute when the swipe gesture is recognized on the newly added transparent subview.

How can I add Swipe Gesture to AVPlayer in swift 3

There are two mistakes on your code.

1. Adding gesture view on wrong controller's view. Instead of adding gesture view to AVPlayerViewController() you were adding on initial controller, which will get covered by AVPlayerViewController() after presenting.

3. Wrong selector's target You were making false assumption about target on let swipeRight = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture)) . You were setting target to sView and implementing selector method on ViewController.

Here target means the target object for selector method. So Changing target to self (i.e. your view controller) will make your view controller a target for selector method func respondToSwipeGesture(gesture: UIGestureRecognizer)

please refer the following corrected code.

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController , UIAlertViewDelegate {

let myFirstButton = UIButton()
let mySecondButton = UIButton()
var scoreLabel = UILabel()
var Player = AVPlayer()
var swipeGesture = UIGestureRecognizer()
var sView = UIView()


override func viewDidLoad() {
super.viewDidLoad()


let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.sView.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.sView.addGestureRecognizer(swipeLeft)

let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeUp.direction = UISwipeGestureRecognizerDirection.up
self.sView.addGestureRecognizer(swipeUp)

let swipeCustom = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeCustom.direction = UISwipeGestureRecognizerDirection.init(rawValue: 200)
self.sView.addGestureRecognizer(swipeCustom)


let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.sView.addGestureRecognizer(swipeDown)


//////////////////////End Swipe Gesture

let currentPlayerItem = Player.currentItem
let duration = currentPlayerItem?.asset.duration
let currentTime = Float(self.Player.currentTime().value)


if currentTime >= 5 {

print("OK")

}else if currentTime <= 5 {

print("NO")
}

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.Player.seek(to: kCMTimeZero)
self.Player.play()
}
})



/////////////
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.Player.seek(to: kCMTimeZero)
self.Player.play()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0) {
// check if player is still playing
if self.Player.rate != 0 {
print("OK")
print("Player reached 5 seconds")
}
}
}
})

}


fileprivate var firstAppear = true
//////Swipe Gesture
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
case UISwipeGestureRecognizerDirection.init(rawValue: 200):
print("Swiped Custom")

default:
break
}
}
}
/////////End Swipe Gesture
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
if firstAppear {
do {
try playBackgroundMovieVideo()
firstAppear = false
} catch AppError.invalidResource(let NMNF6327, let m4v) {
debugPrint("Could not find resource \(NMNF6327).\(m4v)")
} catch {
debugPrint("Generic error")
}


}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
return UIInterfaceOrientationMask.landscapeLeft
}
fileprivate func playBackgroundMovieVideo() throws {
guard let path = Bundle.main.path(forResource: "NMNF6327", ofType:"m4v") else {
throw AppError.invalidResource("NMNF6327", "m4v")

}

self.Player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.showsPlaybackControls = false
playerController.view.isUserInteractionEnabled = true
playerController.player = self.Player
playerController.viewWillLayoutSubviews()
playerController.allowsPictureInPicturePlayback = false



myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
myFirstButton.frame = CGRect(x: 5, y: 5, width: 70, height: 50)
self.myFirstButton.addTarget(self, action:#selector(self.myFirstButtonpressed), for: .touchUpInside)
self.view.addSubview(myFirstButton)


mySecondButton.setImage(#imageLiteral(resourceName: "Options.png"), for: UIControlState.normal)
mySecondButton.frame = CGRect(x: 60, y: 5, width: 70, height: 50)
self.mySecondButton.addTarget(self, action:#selector(self.mySecondButtonClicked), for: .touchUpInside)
self.view.addSubview(mySecondButton)


sView.frame = self.view.frame
playerController.view.addSubview(sView)
playerController.view.bringSubview(toFront: sView)

// ****** buttons are added after sview **********
playerController.view.addSubview(myFirstButton)

playerController.view.addSubview(mySecondButton)

self.present(playerController, animated: false) {
self.Player.play()
}
}

func playerDidReachEnd(notification: NSNotification) {
self.Player.seek(to: kCMTimeZero)
self.Player.play()
}

func myFirstButtonpressed(sender: UIButton!) {
myFirstButton.setImage(#imageLiteral(resourceName: "Play.png"), for: UIControlState.normal)

let alertView = UIAlertView();
alertView.addButton(withTitle: "Continue");
alertView.delegate=self;
alertView.addButton(withTitle: "restart");
alertView.addButton(withTitle: "Middle");
alertView.title = "PAUSE";
alertView.message = "";
alertView.show();

let playerController = AVPlayerViewController()
playerController.viewWillLayoutSubviews()
self.present(playerController , animated: true)
self.Player.pause()

}

func mySecondButtonClicked(){

}





func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {


if buttonIndex == 0
{

self.Player.play()
myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
print("Continue")


}

else if buttonIndex == 1 {
self.Player.seek(to: kCMTimeZero)
self.Player.play()
//myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)


}
////Middle
else if buttonIndex == 2 {
myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
let timeScale = self.Player.currentItem?.asset.duration.timescale;
let time = CMTimeMakeWithSeconds( +9 , timeScale!)
self.Player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
self.Player.play()
}

}

override var shouldAutorotate: Bool{
return false
}
func update() {
myFirstButton.isHidden=false
}

}


enum AppError : Error {
case invalidResource(String, String)
}

Swift, swipe gesture recognizer direction doesn’t work, can’t seem to get the direction

You need to detect one direction at a time. I would also suggest to add a method to control each swipe direction. For example.

func setUpGestures() {
// Gesture that define a left swipe.
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(Scene.swipeLeft))
swipeLeft.direction = .left
view?.addGestureRecognizer(swipeLeft)
// Do the same for the rest of the directions.
}

@objc func swipeLeft() {
// Do something when the user swipe left.
}

Hope it helps!

Problems swiping right with UISwipeGestureRecognizer (Swift 3.0)

I don't know if you dragged it on the view that should listen to the gesture. Check the links pane to see differences between them and link between the right gesture to the view
Maybe the first gesture recognizer disables the second, try to disable the first and see if it helps.. You should set them to forward touches to other recognizers

EDIT: as you said, you should've add a connection to referencing outlets
Glad I could help you (I had this problem at the past) good luck!

How to recognize swipe in all 4 directions

You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:

override func viewDidLoad() {
super.viewDidLoad()

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

if let swipeGesture = gesture as? UISwipeGestureRecognizer {

switch swipeGesture.direction {
case .right:
print("Swiped right")
case .down:
print("Swiped down")
case .left:
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}

Swift 3:

override func viewDidLoad() {
super.viewDidLoad()

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
}

Swift 3 - Swipe left to change label by using Array

Try this:

if sender.direction == .left {
currentArrayIndex = (currentArrayIndex + 1) % 3
helloLabel.text = helloArray[currentArrayIndex]
}

if sender.direction == .right {
currentArrayIndex = (currentArrayIndex + 3 - 1) % 3 //if uInt
helloLabel.text = helloArray[currentArrayIndex]
}


Related Topics



Leave a reply



Submit