Move Multiple images in iOS - swift

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.

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
}
}
}

How to prevent back button from dissapearing?

I am creating a doodle page with an "X" as the close/dismiss button.
I also have a "clear" button to remove the doodles that have been made.
My issue is that when I press clear, even the X button disappears, how do I prevent this from happening?
This is my current app.
This is my Doodle class which allows me to draw on the view.
import UIKit
class DoodleView: UIView {
var lineColor:UIColor!
var lineWidth:CGFloat!
var path:UIBezierPath!
var touchPoint:CGPoint!
var startingPoint:CGPoint!
override func layoutSubviews() {
self.clipsToBounds = true
self.isMultipleTouchEnabled = false
lineColor = UIColor.white
lineWidth = 10
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
startingPoint = touch?.location(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
touchPoint = touch?.location(in: self)
path = UIBezierPath()
path.move(to: startingPoint)
path.addLine(to: touchPoint)
startingPoint = touchPoint
drawShapeLayer()
}
func drawShapeLayer() {
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = lineWidth
shapeLayer.fillColor = UIColor.clear.cgColor
self.layer.addSublayer(shapeLayer)
self.setNeedsDisplay()
}
func clearCanvas() {
path.removeAllPoints()
self.layer.sublayers = nil
self.setNeedsDisplay()
}
}
This is the class controlling the view controller.
import UIKit
class DoodlePageViewController: UIViewController {
#IBOutlet weak var doodleView: DoodleView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func clearDoodle(_ sender: Any) {
doodleView.clearCanvas()
}
#IBAction func CloseDoodlePage(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
Without really seeing how you declared the buttons, I can only guess that when you call the function clearCanvas(), the X button is part of the sublayer in doodle view since you are setting self.layer.sublayers = nil hence making it also dissapear.
Make sure that the X button is on top of the doodle view when you create it.
You are clearing all the subviews out of your view when you call self.layer.sublayers = nil
you either need to re-add the 'x' view back into the hierarchy. I am assuming using your function drawShapeLayer() is the 'x' ??
so it would be
func clearCanvas() {
path.removeAllPoints()
self.layer.sublayers = nil
drawShapeLayer()
self.setNeedsDisplay()
}

UIImageView UIImage String Value Swift 4

I'm trying to get the String value from a UIImageView's UIImage when touchesBegan.
here is the entire viewcontroller code.
import UIKit
class ViewController: UIViewController {
//MARK: Outlets
#IBOutlet weak var imageOne: UIImageView!
#IBOutlet weak var imageTwo: UIImageView!
#IBOutlet weak var myText: UILabel!
let imagesArray = ["banaan", "auto", "kip", "boom" ]
var questionArray: [String] = []
var touchArray: [CGPoint] = []
var itemOne: Int!
var itemTwo: Int!
override func viewDidLoad() {
super.viewDidLoad()
setupQuestions()
setupImages()
}
//MARK: Randomizing The Lable Question
fileprivate func setupQuestions() {
let firstRandomizer = arc4random_uniform(UInt32(imagesArray.count))
let secondRandomizer = arc4random_uniform(UInt32(imagesArray.count))
itemOne = Int(firstRandomizer)
itemTwo = Int(secondRandomizer)
if itemOne == itemTwo && itemTwo != 0 {
itemTwo -= 1
} else if itemOne == itemTwo && itemTwo == 0 {
itemTwo += 1
}
myText.text = """
selecteer de \(imagesArray[itemOne].capitalized)
voor dat je de \(imagesArray[itemTwo].capitalized) selecteert
"""
}
//Mark: setup images and randomizeing
fileprivate func setupImages() {
imageOne.image = UIImage(named: imagesArray[itemOne])
imageTwo.image = UIImage(named: imagesArray[itemTwo])
}
//MARK: Button Randomizer
#IBAction func nextBtnWasPressed(_ sender: Any) {
setupQuestions()
setupImages()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//Get image stringvalue to compare it with the question array
}
}
You can try
self.myImageView.image = UIImage(myImageArray[0])
self.myImageView.tag = 0
then inside touches
myImageArray[self.myImageView.tag]
you can also declare a var for a good pattern following
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("itemOne name \(myImageArray[itemOne]) , itemTwo name \(myImageArray[itemTwo])")
}

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.

UIImageView Zoom and Touch Image

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)
}
}