Touch Gesture on image is not working in ScrollView? - Swift - swift

This is the image on the scroll view where I'm able create those rectangles from superview
This is the image I want how to look my output when I touch to began creating multiple rectangles
I'm trying to create multiple annotations on images using views where it's been achieved. And for the next step trying to zoom in through image view to annotate where it's possible with pinch gesture but the constraint is not able scroll left or right while zoomed in. I have to zoom out and then again have to zoom in to the concentrated area or co-ordinate to annotate again. I gone through few examples and found to be possible of moving or scrolling left or right while zoomed in using scroll view, where I have achieved it. But not able to annotate the image by creating views. When I tried to create it's not happening still accidently I srolled it from superview where it has created the view. Thank you in adavnce.
Class ViewController : UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate{
// Outlets
#IBOutlet weak var displayImageView : UIImageView!
#IBOutlet weak var imageScrolling : UIScrollView!
#IBOutlet weak var containerView : UIView!
// View Did Load
override func viewDidLoad() {
super.viewDidLoad()
displayImageView.isMultipleTouchEnabled [](https://i.stack.imgur.com/ba2n7.png)= false
displayImageView.isUserInteractionEnabled = true
imageScrolling.minimumZoomScale = 1.0
imageScrolling.maximumZoomScale = 4.0
imageScrolling.delegate = self
imageScrolling.contentSize = displayImageView.bounds.size
imageScrolling.isUserInteractionEnabled = true
containerView.isUserInteractionEnabled = true
imageScrolling.isExclusiveTouch = true
imageScrolling.panGestureRecognizer.isEnabled = true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
// View For Zooming - Scroll View Image Zooming and Scrolling
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return displayImageView
}
}
I have also added the screenshot of how it's happening while touch began from superview and how i wanted it to be.
I have also added views hierarchy

Related

ScrollView Constraint problem threw storyboard

So I maybe successfully created a scroll view following a tutorial but something is terribly wrong. So I'm trying to create a scroll view with a table view inside and some buttons and labels and everything works fine but as soon as I add a single constraint it all goes just white with no explanation. I would assume content view is messing things up but I'm not sure, thx in advance!
So following some other people problems I tried filling constraints programmatically and doing views and subviews programmatically aswell, keep in mind tho that rest of constraints I did on storyboard. Btw I have tried equal width and height on newView --> scroll view and nothing seems to change.
my view hierarchy looks like this
myViewController -> View -> scrollView -> newView
class ThirdViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.addSubview(newView)
newView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
newView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
newView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
newView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
//tableView1.dataSource = self
//tableView1.delegate = self
//tableView1.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
}
#IBOutlet weak var newView: UIView!
override func viewWillLayoutSubviews() {
scrollView.contentSize = CGSize(width: 375, height: 1950)
}
}
Sorry, if question is layout pretty badly - I'm not that pro!

UIView's animate method not appearing

I am trying to make an app that utilizes some search feature and I am trying to make it so that after the search button is pressed, a view (which contains the search results) moves up from the bottom of the superview and replaces the search view. On the storyboard, the resultsView (of type UIView) is constrained so that its top is equal to the superview's bottom. After the search button is pressed, I would like to animate the view to move up and replace the view already at the bottom of the superview. The problem is, in the viewcontroller's class, when I call the resultsView, the animateWithDuration(NSTimeInterval) that is supposed to be associated with the UIView class is not appearing for me. May this be because the view is already constrained in place? Here is the code, simplified for this post:
import UIKit
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate,
MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var distanceLabel: UILabel!
#IBOutlet weak var searchButton: UIButton!
#IBOutlet weak var searchView: UIView!
#IBOutlet weak var resultView: UIView!
#IBOutlet weak var resultNameLabel: UILabel!
#IBOutlet weak var resultDistanceLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
self.resultView.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func sliderAdjusted(_ sender: Any) {
let int = Int(slider.value)
switch int {
case 1:
distanceLabel.text = "1 mile"
default:
distanceLabel.text = "\(int) miles"
}
}
#IBAction func searchButtonPressed(_ sender: Any) {
/*This is where, after calling a search function which is omitted
from this example, I would like to make the resultsView not
hidden and then animate it to sort of eclipse the search view*/
self.resultView.isHidden = false
self.resultView.animate(withDuration: NSTimeInterval)
/*The above line of code is not actually appearing for me.
After typing "self.resultView." the animate function is not being
given to me as an option for my UIView*/
}
}
I will also attach some images of the view controller so you can sort of get the idea. The results view is not visible in this image because its top is constrained to the superview's bottom, thus it is just out of the visible representation of its superview.
The first image is the view controller with the searchView highlighted. This is the view that I would like to be eclipsed by my resultView after the searchButton is pressed.
The second image is the same view controller with the resultView highlighted. As you can see, its top is constrained to be equal to the superview's bottom. This is the view that I would like to animate upwards into the superview and eclipse the searchView after the search button is pressed.
The methods for all the animate family are all class methods. Which means you call them on the class object not an instance.
You are trying to call
class func animate(withDuration: TimeInterval, animations: () -> Void)
so your code needs to look like
UIView.animate(withDuration: 0.5) {
//the things you want to animate
//will animate with 0.5 seconds duration
}
In the particular case it looks like you are trying to animate the height of resultView so you need an IBOutlet to that constraint. You could call it resultViewHeight.
UIView.animate(withDuration: 0.5) {
self.resultViewHeight.constant = theDesiredHeight
self.layoutIfNeeded()
}
Calling layoutIfNeeded() within the closure is the secret sauce to animating auto layout. Without that the animation will just jump to the and point.

UIScrollView pan with 1 finger when zoomed in

I got a scrollView with some sub views added to it. Scrolling and zooming works fine.
What I would like to is when the scrollView is zoomed in, alter the behavior of the 1 finger touch to pan instead of scroll.
Right now if you are zoomed in and move around with 1 finger you can only scroll the zoomed in content. With two fingers you can pan around. This is the behaviour I would like to change.
I haven't found any solution to this issue and I don't really have any ideas except trying to alter the minimum and maximum touches required but it doesn't seem to work.
func scrollViewDidZoom(_ scrollView: UIScrollView) {
scrollView.panGestureRecognizer.maximumNumberOfTouches = 1
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
if scale == 1 {
scrollView.panGestureRecognizer.maximumNumberOfTouches = 2
}
}
I realized the issue I had was that each subview had their own scrollView (in my case my subviews wkWebViews). They were handling their own zoom even though I was trying to zoom from the UIScrollView.
I took these steps to solve this issue.
MyClass: UIViewController, UIScrollViewDelegate {
// This view should handle the zooming
#IBOutlet var scrollView: UIScrollView!
// .. and this view will contain all subviews to be zoomed
#IBOutlet var wrapper: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.delegate = self
self.addSViewsToScrollView()
}
func addViewsToScrollView () {
// MARK: - Step 1
// .. set wkWebView scrollview maximum and minimum zoom scale to 1.
let myArray: [WkWebView]? = [WkWebView(),WkWebView(),WkWebView()]
for view in myArray {
wkWebView.scrollView.maximumZoomScale = 1
wkWebView.scrollView.minimumZoomScale = 1
wkWebView.isUserInteractionEnabled = false // This is key otherwise it wont work
wkWebView.scrollView.isUserInteractionEnabled = false // .. this aswell
}
// MARK: - Step 2
// .. add all views to a wrapper view
for view in myArray {
self.wrapperView.addSubview(view)
}
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.wrapper
}
}
By returning a single view the panning in the zoomed in state works fine. Also the wkWebView scrollViews do not interfere with the zooming anymore.

How to let a textfield hover/float above a mapView

I'm making an app which uses MapView, now I've positioned the textfield above the mapview, like on the image. But I really want to fullscreen the mapview and let a textfield float/hover above it. Does anybody know how I can make something like that? I have searched a lot on the internet but couldn't find any clue.. Hope someone can get me starting.
Right, presumably your map view is pinned on all four sides to its superview? Setup your constraints as follows, not how the mapview is not pinned to the text field, but the superview.
http://imgur.com/a/8YmXL
To add the drop shadow, set the following properties on your UITextField:
textField.layer.shadowOpacity = 1.0
textField.layer.shadowRadius = 0.0
textField.layer.shadowColor = UIColor.blackColor().CGColor
textField.layer.shadowOffset = CGSizeMake(0.0, -1.0)
Adjust the settings until you are happy with the look of the shadow.
To dismiss the keyboard when the user pans around the map is very simple. You just need to implement the delegate. Edit your class to look a bit like this:
class MapViewController: UIViewController, MKMapViewDelegate {
#IBOutlet var mapView: MKMapView!
#IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
textField.resignFirstResponder()
}
}

Set of UIImageView.Image re-positions parent view

I have a UIView and added a UIImageView as child to it. I later change the position of the UIView via another UIButton event.
If I change the image of the UIImageView the UIView - inc. all shields - moves back to the initial position.
Could anyone maybe explain me what is going on here :-D How do I keep the new position of the UIView ?
Here a image screen I took to visualize the issue
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var number: UIImageView!
#IBOutlet weak var popup: UIView!
#IBAction func openPopup(sender: UIButton) {
self.popup.center = sender.center
//old position: (190.5, 124.0) - new Position: (173.0, 414.0)
}
#IBAction func updateImage(sender: UIButton) {
self.number.image = UIImage(named: "img-1")
}
}
Update:
I found out that above code works if I deactivate autolayout. Here is a solution to do this with auto layout:
Auto Layout center UIView above another UIView at its center
Are you sure of that? Did you check by logging popup.frame.origin after updateImage being called? Have you properly set clipsToBounds and contentMode properties for number uiimageview?
I found out that above code works if I deactivate "Use Auto Layout" in Interface Builder Document section at tab File Inspector.
In addition here is a working solution to the issue using auto layout and constraints:
Auto Layout center UIView above another UIView at its center