UIImageView Zoom and Touch Image - swift

I have a big image and I want to show this image in ScrollView and I want to detect touched area with x and y coordinates of the image.
If I choose 'User Interaction Enabled' of ScrollView, zoom is working but touchesBegan method doesn't working on ScrollView(It's working outside),
If I didn't choose 'User Interaction Enabled' of ScrollView, zoom isn't working but touchesBegan method working on ScrollView and I'm getting coordinates but I can't zoom.
How can I solve this problem ?
Code :
class ViewController: UIViewController,UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
self.scrollView.minimumZoomScale=1.0;
self.scrollView.isScrollEnabled = true
self.scrollView.maximumZoomScale=7.0;
self.scrollView.contentSize=CGSize(width: 1424,height: 1410);
self.scrollView.delegate=self;
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self.imageView)
print(position.x)
print(position.y)
}
}

Related

On pressed touch Hide/Show UIImage, and count number down from 10 to 0

On every pressed touch I want UILabel to count number down one by one from 10 to 0.
Also when pressed I want to show UIImageView: "MyRocketWith", and when release the pressed touch I want to show UIImageView: "MyRocket".
class ViewController: UIViewController {
#IBOutlet weak var MyRocket: UIImageView!
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var Countdown: UILabel!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch? = touches.first
if touch?.view != self.MyRocket {
self.MyRocketWith.isHidden = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
The below code shows and hides the UIImageView on click of UILabel
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var MyRocketWith: UIImageView!
#IBOutlet weak var CountDownLabel: UILabel!
#IBOutlet weak var MyRocket: UIImageView!
var count = 10
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
CountDownLabel.isUserInteractionEnabled = true
CountDownLabel.addGestureRecognizer(tap)
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
#IBAction func tapFunction(sender: UITapGestureRecognizer) {
print("tap working")
count = count-1
CountDownLabel.text = String(count)
if MyRocketWith.tag == 0
{
MyRocket.isHidden = true
MyRocketWith.isHidden = false
MyRocketWith.tag=1
}
else
{
MyRocket.isHidden = false
MyRocketWith.isHidden = true
MyRocketWith.tag=0
}
}
}

dismiss keyboard tapping background with action

My app has two UITextFields. Both I have dismissing by tapping the background because they are using the decimal keyboard. I want it so if say textField1 is up and you tap the background a specific button pops up with the actions for textField1 and vice versa. Currently if you tap the background with out any keyboard up it still pops up a button. Any help would be awesome thanks.
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var button: UIButton!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
button.isHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
button.isHidden = true
}

Bring drawing UIView in front of image

I'm trying to code an app which in part involves using one's finger to trace an object in a photograph. I've got two relevant files:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var image_edit: UIImageView!
var rec_houseimages: [UIImage]?
override func viewDidLoad() {
super.viewDidLoad()
image_edit.image = rec_houseimages?[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and this one:
import UIKit
class Draw: UIView {
var touch : UITouch!
var lineArray : [[CGPoint]] = [[CGPoint]()]
var index = -1
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first! as UITouch
let lastPoint = touch.location(in: self)
index += 1
lineArray.append([CGPoint]())
lineArray[index].append(lastPoint)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first! as UITouch
let currentPoint = touch.location(in: self)
self.setNeedsDisplay()
lineArray[index].append(currentPoint)
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
if(index >= 0){
context!.setLineWidth(5)
context!.setStrokeColor((UIColor(red:0.00, green:0.38, blue:0.83, alpha:1.0)).cgColor)
context!.setLineCap(.round)
var j = 0
while( j <= index ){
context!.beginPath()
var i = 0
context?.move(to: lineArray[j][0])
while(i < lineArray[j].count){
context?.addLine(to: lineArray[j][i])
i += 1
}
context!.strokePath()
j += 1
}
}
}
When I run my program, I get my desired image placed on the screen and the drawing code executes fine; but the image is in front of the drawing so that you can only see the lines if you draw outside of the image. I've been looking up things like bringSubviewToFront but I'm not sure how to implement them with this drawing stuff.

Move Multiple images in iOS

I have multiple images in a view controller. I am trying to implement the touch method to drag the images. When I try to drag a single image view all the image views are dragging. Here is the code I am using:
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
#IBOutlet weak var img4: UIImageView!
var location = CGPoint(x: 0 , y:0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
location = touch.locationInView(self.view)
if touch.view == img1 {
print("img1 tapped")
img1.center = location
}
else if touch.view == img2 {
print("img2 tapped")
img2.center = location
}
else if touch.view == img3 {
print("img3 tapped")
img3.center = location
}
}
super.touchesBegan(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
location = touch.locationInView(self.view)
if touch.view == img1 {
print("img1 tapped")
img1.center = location
}
else if touch.view == img2 {
print("img2 tapped")
img2.center = location
}
else if touch.view == img3 {
print("img3 tapped")
img3.center = location
}
}
}
you need to take array of UIimageView and then set recognizer. So each and every recognizer wil be set in NSUser Default.

UIPanGestureRecognizer does not work

It's weird, but when I add a UIPanGestureRecognizer to a view it does not work. It's my view, that white view is a subview of the red view:
Main View:
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.gestureRecognizers = [panRecognizer]
}
func handlePan(recognizer:UIPanGestureRecognizer) {
//foo
}
When I tap and drag the white view it won't move.
try this:
whiteView.isUserInteractionEnabled = true
From the Apple's docs:
If the code for your pan gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.
The number of touches is between the values specified in the minimumNumberOfTouches and maximumNumberOfTouches properties.
For a UIScreenEdgePanGestureRecognizer object, the edges property is configured and touches start at the appropriate edge.
Handling Pan Gestures
Here is the answer:
class ViewController: UIViewController {
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
var lastLocation:CGPoint = CGPointMake(0, 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.addGestureRecognizer(panRecognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(redView)
whiteView.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
lastLocation = whiteView.center
}
}
If you are using swift language for method you can't use [].
var PanGesture = UIPanGestureRecognizer(target: self, action: "respondToPanGesture:")
whiteView.addGestureRecognizer(PanGesture)
Hope it helps.