How can I load a website by clicking a UIImage View? - swift

I have created a UI Image view. Is it possible for the user to click on the UI Image view and have the link pop up? If so how would I go about incorporating that? I was thinking possibly through my View Controller using Touches Began and then specifically with a tag, but I'm using Swift's Game View Controller so the Touches Began function is not working properly and won't allow me to convert my SKNODE to Game View Controller type. Any suggestions on how you would do this? Thanks

You are going to want to look at UITapGestureRecognizer. Here is an example below.
if let imageView = yourImageView {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.openLink(_:)))
imageView.addGestureRecognizer(tap)
}
Here is a link if you are also wondering how to open a link in safari.
Swift Open Link in Safari

I just used a gesture recognizer and it worked perfectly

Check out this :
let recognizer = UITapGestureRecognizer(target: self, action: #selector(webViewTapped))
recognizer.delegate = self
webView.addGestureRecognizer(recognizer)
and :
#objc func webViewTapped(_ recognizer: UITapGestureRecognizer) {
if let selectedWebView = recognizer.view as? WKWebView {
selectWebView(selectedWebView)
}
}
func selectWebView(_ webView: WKWebView) {
activeWebView = webView
updateUI(for: webView)
}

Related

How to call a function when the Compass Button of a mapView is pressed

I have developed an app showing a map view with a compass button. The natural function when pressing this compass button is that the map is being oriented to north.
Now I want to call a function when pressing this compass button.
A view of the app with a map can be found there (compass button is visible in the upper right corner)
http://aceingolf.com/wp-content/uploads/2018/12/0.jpg
The code below is working fine. It is just shown to give a complete view.
#IBOutlet weak var mapView: MKMapView!
let compassButton = MKCompassButton(mapView: mapView)
compassButton.compassVisibility = .visible
mapView.addSubview(compassButton)
I am looking for a way to create an action when the user presses the compass button on the map view.
I believe MKCompassButton inherits from UIView, So you can just add a UITapGestureRecognizer to the compassButton with an action like following:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleCompassTap))
compassButton.addGestureRecognizer(tapGestureRecognizer)
#objc func handleCompassTap() {
// Implement your feature when the compass button tapped.
}
Take emrepuns code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleCompassTap))
compassButton.addGestureRecognizer(tapGestureRecognizer)
#objc func handleCompassTap() {
// Implement your feature when the compass button tapped.
}
and add this line
tapGestureRecognizer.numberOfTouchesRequired = 1
or
tapGestureRecognizer.numberOfTapsRequired = 1

Swift: Segue to another view when Map View is tapped

I would like to perform a segue to another view once a map view is tapped like when you check a location and tap on map on instagram, or like in foursquare.
I could not connect map view itself to another view directly like we do in buttons, and I don't want to perform segue when callout button is tapped.
I found a workaround by placing an invisible button into mapView, and performing segue when the button is tapped(user thinks mapView is tapped)
But I would like to know if there is a better solution for it, without a button.
You can use UITapGestureRecognizer
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapHandler(_:)))
mapView.addGestureRecognizer(gestureRecognizer)
#objc func tapHandler(_ gestureReconizer: UIGestureRecognizer) {
// let location = gestureReconizer.location(in: mapView)
// coordinatte of taplocation
// let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
// **you can navigate or perform Your Actions there**
}

Disable swipe to close on AVPlayerController

iOS 11 has introduced a swipe to close AVPlayerController. I have app that is aimed at toddlers so the screen is easily swiped causing the video to close. Is there anyway to remove the gesture to close the player?
I have tried adding a gesture override to the AVPlayerController's view but it doesn't work. There is a possible solution on How can I add Swipe Gesture to AVPlayer in swift 3 but there must be a cleaner way
If AVPlayerController is embedded (not presenting) the Close button is not presented in the controls view.
My solution is to find the subview with gesture recognizers and remove pan gesture recognizer
for v in playerViewController.view.subviews {
if v.gestureRecognizers != nil {
for gr in v.gestureRecognizers! {
if gr is UIPanGestureRecognizer {
// remove pan gesture to prevent closing on pan
v.removeGestureRecognizer(gr)
}
}
}
}
I managed to solve issue. As #Vakas commented, the AVPlayerController shouldn't be subclassed. I had originally subclassed it and presented using a modal segue. This was causing the problem.
To solve it, I created another view controller that embeds the AVPlayerController in it.
import UIKit
import AVKit
class PlayerViewController: UIViewController, AVPlayerViewControllerDelegate {
var videoRecord: Video!
var presentingController = ""
var videos = [Video]()
var presentingPlaylist: Playlist?
let playerViewController = TFLPlayerController()
override func viewDidLoad() {
super.viewDidLoad()
playerViewController.delegate = self
playerViewController.videoRecord = videoRecord
playerViewController.videos = self.videos
playerViewController.allowsPictureInPicturePlayback = false
// Add the original AVPlayerController in here
self.addChildViewController(playerViewController)
let playerView = playerViewController.view
playerView?.frame = self.view.bounds
self.view.addSubview(playerView!)
playerViewController.didMove(toParentViewController: self)
}
}
I basically use this View Controller to pass through the properties such as videos, etc, to the originally subclassed AVPlayerController.
None of the comments above solved the issue (iOS 13+). Solution:
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
avPlayerViewController.view.addGestureRecognizer(panGestureRecognizer)
where handlePanGesture(_:) is the method that will be called if a pan happen on the screen (and the video won't move - that was the problem in the question that it was dragged), and avPlayerViewController is the AVPlayerViewController instance.
Note: if you want to prevent the pinch / rotation and any other gesture, you can add for every gesture a new UI...GestureRecognizer. Just make sure, that all UI...GestureRecognizers' delegate is set, and this function is implemented:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

TapGestureRecognizer not firing in swift

Hi I'm having no cards to used to fixed this. Here we go.
I have this five imageview this is the declaration of first imageview but basically they have same declaration
#IBOutlet weak var first: UIImageView!{
didSet{
first.tag = 1
first.addGestureRecognizer(tapGesture())
}
}
and Then the tapGesture() method is this
private func tapGesture() -> UITapGestureRecognizer {
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("iconTapped:"))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
return tapGesture
}
the action when the imageview is tapped the iconTapped() method is called, this is the code
func iconTapped(sender:UITapGestureRecognizer){
if let tag = sender.view?.tag{
processGesture(tag)
}
}
I have put all imageview userInteractionEnable to true but still not firing. Could anyone help me?
Some factor may or may not help, inside the viewdidload I update constraints and do some animation to imageview, but I'm not adding any subview programmatically
after trying so many things, I look on my view hierarchy and figure out that the parent view where my UIImageView rest userInteractionEnable is set to false(unchecked) in storyboard.
in this case the parentview is my superview, to fixed that I need to add this in viewDidLoad()
self.view.userInteractionEnabled = true
or
click the parentView in storyboard and in attributes inspector check the User Interaction Enable.
ps. sorry for silly mistake but I'll keep this question posted there might be other encountering same problem.

How can I find button tag after implementing gesture recogniser in swift?

I want an action to be done once when someone swipes within a button.
My current code is as follows:
let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
recogniser.direction = .Up
button.addGestureRecognizer(recogniser)
func didTapButton2(sender: UIGestureRecognizer!) {
//Here I want to be able to recognise which button this was sent from (they are all tagged)
let button = sender. as UIButton //Gives an error
I need to use gesture recognisers as opposed to UIControlEvents as I need the event to only fire once. Using this makes the event fire loads of times - just need it to go once:
button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)
Does anyone have a solution? Thanks
A UIGestureRecognizer has a property called view which is the view that it is attached to. View is declared as an optional, so you need to unwrap it:
if let button = sender.view as? UIButton {
// use button
if button.tag == 10 {
// handle button tagged with 10
}
}