how to add GestureRecognizer for PDFAnnotation ios swift 4 - swift4

it's possible to add GestureRecognizer or set on touch to PDFAnnotation
func setDocumentAnnotation() {
let anotation:PDFAnnotation = PDFAnnotation(bounds: CGRect(x: pointX, y: pointY, width: pointW, height: pointH), forType: .highlight, withProperties: nil)
anotation.color = .yellow
anotation.endLineStyle = .circle
guard let page = pdfView.currentPage else {return}
page.addAnnotation(anotation)
}
#objc func annotationTapping(_ sender: UITapGestureRecognizer){
print("------- annotationTapping ------")
}

Hopefully this method will work for you..
NotificationCenter.default.addObserver(self, selector: #selector (titleTapped), name:Notification.Name.PDFViewAnnotationHit , object: nil)
#objc func titleTapped() {
-Provide the action you want---
}

It's not possible to add the gesture recognizer to the annotation itself. However you can add it to the PDFView and then check if the user tapped on an annotation. Here's a simple snippet:
let tappedPage = pdfView.page(for: tapLocation, nearest: false)
if let page = tappedPage {
let tapPageLocation = pdfView.convert(tapLocation, to: page)
let annotation = page.annotation(at: tapPageLocation)
}
I hope this helps.

Related

"unrecognized selector sent to instance" error when adding ViewController

I've recently attempted to add a welcome screen to my AR app that works as a Home screen. When the app loads, the user can tap the button and then the app freezes, crashes and displays the code
"Thread 1: "-[_0_2_2020_2.WelcomeViewController letsGo:]: unrecognized selector sent to instance 0x13ec05e00"
I've tried a few of the solutions available, but I haven't been able to come up with a solution. I think it has something to do with my *IBAction connection. Any assistance is greatly appreciated!
import UIKit
import RealityKit
import ARKit
class WelcomeViewController: UIViewController {
#IBAction func gotPressed(_ sender: Any) {let storyboard = UIStoryboard(name: "Main",
bundle: nil)
if let viewController = storyboard.instantiateViewController(withIdentifier:
"ViewController") as? ViewController {
self.present(viewController, animated: true, completion: nil) /// present the view
controller (the one with the ARKit)!
} }
}
class ViewController: UIViewController, ARSessionDelegate {
//delay app launch to show splash screen
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 3.0)
// Override point for customization after application launch.
return true
}
//end splash screen delay
#IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
showModel()
overlayCoachingView()
setupARView()
arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))))
}
func showModel(){
let anchorEntity = AnchorEntity(plane: .horizontal, minimumBounds:[0.7, 0.7])
anchorEntity.scale = [0.2, 0.2, 0.2]
let entity = try! Entity.loadModel(named: "COW_ANIMATIONS")
entity.setParent(anchorEntity)
arView.scene.addAnchor(anchorEntity)
}
//Overlay coaching view "adjust iphone scan"
func overlayCoachingView () {
let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0, y: 0, width: arView.frame.width, height: arView.frame.height))
coachingView.session = arView.session
coachingView.activatesAutomatically = true
coachingView.goal = .horizontalPlane
view.addSubview(coachingView)
}//end overlay
func setupARView(){
arView.automaticallyConfigureSession = false
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
configuration.environmentTexturing = .automatic
arView.session.run(configuration)
}
//object placement
#objc
func handleTap(recognizer: UITapGestureRecognizer){
let location = recognizer.location(in:arView)
let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
if let firstResult = results.first {
let brownCowAnchor = ARAnchor(name: "COW_ANIMATIONS", transform: firstResult.worldTransform)
arView.session.add(anchor: brownCowAnchor)
} else {
print("Object placement failed - couldn't find surface.")
//cow animations
//let robot = try! ModelEntity.load(named: "COW_ANIMATIONS")
let brownCowAnchor = AnchorEntity()
let blackCowAnchor = AnchorEntity()
//anchor.children.append(robot)
//arView.scene.anchors.append(anchor)
//robot.playAnimation(robot.availableAnimations[0].repeat(duration: .infinity),
//transitionDuration: 0.5,
//startsPaused: false)
//start cow animation
let brownCow = try! ModelEntity.load(named: "COW_ANIMATIONS")
let blackCow = try! ModelEntity.load(named: "Cow")
brownCow.position.x = -1.0
blackCow.position.x = 1.0
brownCowAnchor.position.z = -2.0
blackCowAnchor.position.z = -2.0
brownCow.setParent(brownCowAnchor)
blackCow.setParent(blackCowAnchor)
arView.scene.anchors.append(brownCowAnchor)
arView.scene.anchors.append(blackCowAnchor)
let cowAnimationResource = brownCow.availableAnimations[0]
let horseAnimationResource = blackCow.availableAnimations[0]
brownCow.playAnimation(cowAnimationResource.repeat(duration: .infinity),
transitionDuration: 1.25,
startsPaused: false)
blackCow.playAnimation(horseAnimationResource.repeat(duration: .infinity),
transitionDuration: 0.75,
startsPaused: false)
//end cow animations
}
}
func placeObject(named entityName: String, for anchor: ARAnchor) {
let entity = try! ModelEntity.loadModel(named: entityName)
entity.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation, .translation], for: entity)
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(entity)
arView.scene.addAnchor(anchorEntity)
}
}
The button is triggering a function letsGo which doesn't appear anywhere on the WelcomeViewController you posted. Check interface builder and make sure that you've removed the old connection from the button. Should be the final tab.

Gesture recognizer is not working on UIView

I'm trying to get a few gesture recognisers to work on a UIView. The UIview is in this case a retrieved SVG image, the library that I use is SwiftSVG.
But the actual added image is a UIView, so I think that is not the problem?
override func viewDidLoad() {
super.viewDidLoad()
let svgURL = URL(string: "https://openclipart.org/download/181651/manhammock.svg")!
let hammock = UIView(SVGURL: svgURL) { (svgLayer) in
svgLayer.fillColor = UIColor(red:0.8, green:0.16, blue:0.32, alpha:1.00).cgColor
svgLayer.resizeToFit(self.v2imageview.bounds)
}
hammock.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
hammock.isUserInteractionEnabled = true;
hammock.addGestureRecognizer(tap)
self.view.addSubview(hammock)
}
// function which is triggered when handleTap is called
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
How can I make the recognizer work?
Thanks
You need to set a frame
hammock.frame = ///
or constraints

why is bringSubviewToFront() only working for panning, not tapping?

view.bringSubviewToFront(tappedView)
is working when I drag (pan) views but not when I tap them. My issue is that when I have one view layered over the other, I want the bottom view to come to the front when tapped. Currently it will only come to the front when dragged. Do I need some additional code to do this? Thanks.
Here's an excerpt from my code for more context:
#objc func didPan(_ recognizer: UIPanGestureRecognizer) {
let location = recognizer.location(in: self.view)
switch recognizer.state {
case .began:
currentTappedView = moviePieceView.filter { pieceView -> Bool in
let convertedLocation = view.convert(location, to: pieceView)
return pieceView.point(inside: convertedLocation, with: nil)
}.first
currentTargetView = movieTargetView.filter { $0.pieceView == currentTappedView }.first
case .changed:
guard let tappedView = currentTappedView else { return }
let translation = recognizer.translation(in: self.view)
tappedView.center = CGPoint(x: tappedView.center.x + translation.x, y: tappedView.center.y + translation.y)
recognizer.setTranslation(.zero, in: view)
view.bringSubviewToFront(tappedView)
```
I had this same problem, I managed to solve it this way:
Add a gestureRecognizer to your view and put this in your code. Don't forget to set the delegate as well when attaching this to the code!
#IBAction func tappedView1(recognizer: UITapGestureRecognizer) {
view.bringSubviewToFront(myView)
}
Put this in your viewDidLoad:
let tap = UITapGestureRecognizer(target: self, action: #selector(tappedView1))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
If you want to do that from your panGesture check my question.
Thank you all. It worked when I wrote a second function in addition to didPan().
This is the additional code:
override func viewDidLoad() {
super.viewDidLoad()
configureLabel()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didPan(_:)))
view.addGestureRecognizer(panGestureRecognizer)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
view.addGestureRecognizer(tapGestureRecognizer)
}
#objc func didTap(_ recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self.view)
currentTappedView = moviePieceView.filter { pieceView -> Bool in
let convertedLocation = view.convert(location, to: pieceView)
return pieceView.point(inside: convertedLocation, with: nil)
}.first
guard let tappedView = currentTappedView else { return }
view.bringSubviewToFront(tappedView)
}
HOWEVER, I found out you can also just tick the "User Interaction Enabled" box in the Image View if you don't want to do it programmatically.

Unable to add swipe recognizer to SKScene in Swift playground?

Ok, Im still a little new to how swift playgrounds work but I am trying to add a swipe gesture recognizer in Swift 3 to my swift playground. Following this http://www.spritekitlessons.com/gesture-recognizer-with-sprite-kit-and-swift/ I now have:
func swipedRight(sender:UISwipeGestureRecognizer){
print("swiped right")
}
func swipedLeft(sender:UISwipeGestureRecognizer){
print("swiped left")
}
func swipedUp(sender:UISwipeGestureRecognizer){
print("swiped up")
}
func swipedDown(sender:UISwipeGestureRecognizer){
print("swiped down")
}
let degree = CGFloat(M_PI_2) / 90
class GameScene: SKScene {
var selectedNode: SKNode?
var shakeAction: SKAction?
override func didMove(to view: SKView) {
/* Setup your scene here */
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedRight:")))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedLeft:")))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedUp:")))
swipeUp.direction = .up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector(("swipedDown:")))
swipeDown.direction = .down
view.addGestureRecognizer(swipeDown)
}
let frame = CGRect(x: 0, y: 0, width: 1000, height: 600) //view size
let view = SKView(frame: frame)
let scene = GameScene(size: frame.size)
view.presentScene(scene)
PlaygroundPage.current.liveView = view
This compiles, however when I swipe I get an unrecognized selector error even though I did include the functions with the selector:
I have tried placing the functions within the class as well. How can I add a swipe recognizer to a Swift playground SKScene?
You have selectors passed as strings, something is definitely wrong with them as said in error log
Try to use new selector syntax - #selector(methodName).
Example:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* Swift 3 */
let swipe = UISwipeGestureRecognizer(target: self, action:#selector(handleSwipe))
view.addGestureRecognizer(swipe)
}
func handleSwipe() {
print("Swiped!")
}
}
Using strings for selectors has been deprecated.
Using new selector syntax if the methodName() method doesn't exist, you'll get a compile error – your app won't crash because of "unrecognized selector".

swift uitapgesturerecognizer pass parameters

I have a lot of Buttons created programmatically and that can change anytime. my tap gesture :
let apriVideoGesture = UITapGestureRecognizer(target: self, action: #selector(PrincipaleController.apriVideo(_:)))
cont.addGestureRecognizer(apriVideoGesture)
func apriVideo(sender : UITapGestureRecognizer){
}
how can i pass parameters? somethig like this :
let apriVideoGesture = UITapGestureRecognizer(target: self, action: #selector(PrincipaleController.apriVideo(stringa : "ciao")))
cont.addGestureRecognizer(apriVideoGesture)
func apriVideo(sender : UITapGestureRecognizer, stringa : String){
}
sorry for bad english, i'm italian
First of all, if you are using button then why are you adding tap gesture? You can add target to it as
btn.addTarget(self, action: #selector(self.btnPressed(_:)), forControlEvents: .TouchDragInside)
But still you can achieve you goal using tap gesture as
Using UIView as u have insisted
class ViewController: UIViewController {
let arrayOfSongsURL: [String] = []
let startingTag = 100
override func viewDidLoad() {
super.viewDidLoad()
let height : CGFloat = 100
let width : CGFloat = 100
(arrayOfSongsURL as NSArray).enumerateObjectsUsingBlock { (url, index, finished) -> Void in
let v = UIView(frame: CGRectMake(0, CGFloat(index) * height, width, height))
v.tag = self.startingTag + index
v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture(_:))))
self.view.addSubview(v)
}
// Do any additional setup after loading the view, typically from a nib.
}
#objc func handleTapGesture(gesture : UITapGestureRecognizer)
{
let v = gesture.view!
let tag = v.tag
let songURL = arrayOfSongsURL[tag - startingTag]
//Do what you want to do with songURL
}
}