tap gesture is not working when i touch my player( MPMovieplayerController) - ios

here i try to merge all video and display that video in screen. but i need to add tap gesture when i press the player screen it should handle some action. but if i tab on view its working but if i tap on player its not working. any solution please
EDITED:
class ViewController: UIViewController,UIGestureRecognizerDelegate {
var location: CGPoint!
var gestureRecognizer: UITapGestureRecognizer!
var videoPlayer = MPMoviePlayerController()
override func viewDidLoad() {
super.viewDidLoad()
musicImg.hidden = true
let gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture")
gestureRecognizer.delegate = self;
videoPlayer.view.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
//location = gestureRecognizer .locationInView(videoPlayer.view)
print("tapped")
}
//#pragma mark - gesture delegate
// this allows you to dispatch touches
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Displaying my selected video in screen:
self.mediaUI.dismissViewControllerAnimated(true, completion: nil)
self.videoPlayer = MPMoviePlayerController()
self.videoPlayer.contentURL = self.videoURL
self.videoPlayer.controlStyle = .Embedded
self.videoPlayer.scalingMode = .AspectFill
self.videoPlayer.shouldAutoplay = true
self.videoPlayer.backgroundView.backgroundColor = UIColor.clearColor()
self.videoPlayer.fullscreen = true
self.videoPlayer.view.frame = CGRectMake(38, 442, 220, 106)
self.view.addSubview(self.videoPlayer.view)
self.videoPlayer.play()
self.videoPlayer.prepareToPlay()
My screen in fully white, no button,label are showing

Try with this...
let gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture")
gestureRecognizer.delegate = self;
If you want to add gesture in movie player controller
videoPlayer.view.addGestureRecognizer(gestureRecognizer)
and if you want for main screen...
self.view.addGestureRecognizer(gestureRecognizer)
And implement delegate methods
#pragma mark - gesture delegate
// this allows you to dispatch touches
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return true
}
// this enables you to handle multiple recognizers on single view
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer touch: UITouch) -> Bool {
return true
}

Related

Swipe gesture is not being called on a subview inside a present view controller

I presented a view controller and I have a subview in it. I added a swipe gesture on the subview. The swipe gesture is not being called. Instead, the presented view controller is trying to dismiss itself i.e. going down. How do I override the swipe gesture to be recognised by the subview instead of the super view.
When I implemented swipe on a static view controller, it works as expected.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var viewSwipe: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp(_:)))
swipeUp.direction = UISwipeGestureRecognizer.Direction.up
self.viewSwipe.addGestureRecognizer(swipeUp)
let swipedown = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown(_:)))
swipedown.direction = UISwipeGestureRecognizer.Direction.down
self.viewSwipe.addGestureRecognizer(swipedown)
}
#objc func swipeUp(_ gesture: UISwipeGestureRecognizer) {
print("swiped up")
}
#objc func swipeDown(_ gesture: UISwipeGestureRecognizer) {
print("swiped down")
}
}
The red area needs to get swipe
Add Gesture only in view and you can also add any direction on gesture. Here i put Small example, you can refer this code for your problem.
Get 1 for Right Direction and 2 For Left Direction
Example :
import UIKit
class ViewController: UIViewController,UIGestureRecognizerDelegate{
#IBOutlet weak var viewGray: UIView!
#IBOutlet weak var viewRed: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
gesture.direction = direction
gesture.delegate = self
self.viewRed.addGestureRecognizer(gesture)
}
}
#objc func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
Your code should work. If not there, is probably conflict with different gesture recognizer. To solve this conflict, you can set delegate on your swipeUp and swipeDown recognizers:
class ViewController: UIViewController, UIGestureRecognizerDelegate
swipeUp.delegate = self
swipeDown.delegate = self
and try to investigate what is happening in this delegate funcs (with breakpoints or with print, it is up to you):
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
If none of this delegate funcs is called, when you are trying to swipe. Then there is probably different (ie invisible) view over you content.
you code will work. Just try to present your view controller as a full screen in case iOS 13. Refer following code.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)

How to resolve the Gesture conflict with webView and its subview?

I want to add double click gesture to my webview, but not intercept its own click callback,I a subview to response double click gesture , but click callback of the webview will be invalid
{
webMaskView = UIView()
webVie w.addSubview(webMaskView)
webMaskView.snp.makeConstraints { (make) in
make.left.right.top.bottom.equalTo(webV)
}
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(webViewTwoTaped(_ :)))
doubleTap.delegate = self
doubleTap.numberOfTapsRequired = 2
webMaskView.addGestureRecognizer(doubleTap)
webMaskView.isUserInteractionEnabled = true
webMaskView.backgroundColor = UIColor.blue.withAlphaComponent(0.1)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return true
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
You no need to add subview for double tap gesture, You can add double tap gesture directly on Webview or WKWebView.

UIGestureRecognizer recognize order problem

I have a red view and a green view (green view is a subview of redview),and I add a gestureRecognizer to each of them.
And both grestureRecognizer's shouldRecognizeSimultaneouslyWith return YES.
But when I tap the green view , the redview's gesture delegate method shouldRecognizeSimultaneouslyWith is called first .
Because i think the hit-test view should be the green view . so the green view 's shouldRecognizeSimultaneouslyWith should be the first .
How do the iOS to decide which grestureRecognizer should be recogonized first ?
Post the code and print logs:
class GreenView: UIView,UIGestureRecognizerDelegate{
override func awakeFromNib() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(greenTap))
gesture.delegate = self
self.addGestureRecognizer(gesture)
}
#objc func greenTap(){
print("greenTap")
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("greenTap shouldRecognizeSimultaneouslyWith")
return true
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
print("green gestureRecognizerShouldBegin")
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
}
class RedView: UIView,UIGestureRecognizerDelegate{
override func awakeFromNib() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(redTap))
gesture.delegate = self
self.addGestureRecognizer(gesture)
}
#objc func redTap(){
print("redTap")
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
print("redTap gestureRecognizerShouldBegin")
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("redTap shouldRecognizeSimultaneouslyWith")
return true
}
}
and the prints are :
green gestureRecognizerShouldBegin
redTap gestureRecognizerShouldBegin
green gestureRecognizerShouldBegin
redTap shouldRecognizeSimultaneouslyWith
greenTap shouldRecognizeSimultaneouslyWith
redTap
greenTap
and we can see that the redview(superview) 's shouldRecognizeSimultaneouslyWith and action method is called before the greenview(subview)'s methods
Don't add the green view as subview of red view. Just add the green view outside red view above it and center the constraints to red view

Swift : UIPanGestureRecognizer delegate method not called

I am facing some weird issue, here is my code for registering pan gesture
public func registerGesture(_ view: UIView) {
self.gesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture(_:)))
self.gesture?.minimumNumberOfTouches = 1
self.gesture?.maximumNumberOfTouches = 1
self.gesture?.delegate = self
view.addGestureRecognizer(self.gesture!)
}
UIPanGestureRecognizer delegate method is not get called.
extension PanGestureHandler : UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
guard let g = self.gesture else { return false }
guard g.view is UIScrollView else { return false }
return true
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy
otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
}
Instead, if i debug the code and print the line self.gesture.delegate, then the delegate method is getting called.
Every time i need to print the above line to work. Please help me, thanks
For the above issue, i have fixed by adding below sharedinstace,
static let sharedInstance : PanGestureHandler = {
let instance = PanGestureHandler()
return instance
}()
And registering the pangesture for view by,
let gestureInstance = PanGestureHandler.sharedInstance
gestureInstance.registerGesture(self.view)

Detect UIScreenEdgeGestureRecognizer and fail UIPanGestureRecognizer

In my ViewController I have a UIScreenEdgeGestureRecognizer for switching views but also a UITableView with a custom UITableViewCell. In this custom UITableViewCell is a UIPanGestureRecognizer for swiping the cells.
I added gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: so both of the gestures are working and gestureRecognizerShouldBegin to prevent the conflict of vertical scrolling.
The question is, how can I give the UIScreenEdgeGestureRecognizer priority? When I swipe at the edge of the screen, I want to switch the views without panning the cells. I figured I should be using the delegate methods, but so far the more I read the more confused I'm getting.
Code in custom UITableViewCell:
override func viewDidload() {
var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
recognizer.delegate = self
addGestureRecognizer(recognizer)
}
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGesture = gestureRecognizer as? UIPanGestureRecognizer {
let velocity = panGesture.velocityInView(superview!)
if fabs(velocity.x) > fabs(velocity.y) { return true }
return false
}
return false
}
ViewController:
override func viewDidLoad() {
let rightScreenEdgeRecognizer = EdgePanGesture(target: self, action: "changeView:")
rightScreenEdgeRecognizer.edges = .Right
view.addGestureRecognizer(rightScreenEdgeRecognizer)
}
I also tried identifying the recognizers with:
gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
and
gestureRecognizer as? UIScreenEdgePanGestureRecognizer
But all were failed.
Turns out I was looking at the right direction with gestureRecognizer as? UIScreenEdgePanGestureRecognizer but instead of gestureRecognizer I should have used otherGestureRecognizer.
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if let recognizer = otherGestureRecognizer as? UIScreenEdgePanGestureRecognizer {
return true
}
return false
}

Resources