I have a label called date2 and I want to drag him only to right or left.
I have got 2 issues - first - I have copied a code by the internet but the Xcode show a bug:
#IBOutlet var date2: UILabel!
var location = CGPoint(x: 0, y: 0)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent?) {
let touch : UITouch! = touches.anyObject() as! UITouch
location = touch.locationInView(self.view)
date2.center = location
}
The second thing is - that the code is to move the label anywhere and not only to the right and left.
Try this code.
#IBOutlet var date2: UILabel!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent?) {
let touch : UITouch! = touches.anyObject() as UITouch
var location = date2.center
location.x = touch.locationInView(self.view).x
date2.center = location
}
I think it solves ur both issues.
Related
I need to move an object side to side on the screen with the users touch. I have already made the object move with the users touch but I want to make it so the object or image can only be moved right or left. What code should I use to do this? Here is what I have right now.
import UIKit
class ViewController: UIViewController {
#IBOutlet var Person: UIImageView!
var location = CGPoint (x: 0, y: 0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
Person.center = location
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
Person.center = location
}
override func viewDidLoad() {
super.viewDidLoad()
Person.center = CGPointMake(160, 330)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This should work. You can change the y to change how you want the UIImageView to always stay on but make sure both are same! At the least, this should give you an idea or put you in the right direction.
import UIKit
class ViewController: UIViewController {
#IBOutlet var Person: UIImageView!
var location = CGPoint(x: 0, y: 0)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
let point = location.x
Person.center = CGPoint(x: point, y: 160) // change 160 to set vertical height you desire
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch : UITouch! = touches.first! as UITouch
location = touch!.locationInView(self.view)
let point = location.x
Person.center = CGPoint(x: point, y: 160) // change 160 to set vertical height you desire
}
override func viewDidLoad() {
super.viewDidLoad()
Person.center = CGPointMake(160, 330)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am sure there are better ways but I am still a beginner so...
I want show the x corrdinate of a touched and moved object.
Try it out:
import UIKit
class ViewController: UIViewController {
var location = CGPoint(x: 0, y: 0)
#IBOutlet weak var viewBox: UIView!
#IBOutlet weak var cntInViewBox: UILabel!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
location = touch.locationInView(self.view)
viewBox.center = location
/*cntInViewBox.text=String(location.x)*/
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
location = touch.locationInView(self.view)
viewBox.center = location
/*cntInViewBox.text=String(location.x)*/
}
override func viewDidLoad() {
super.viewDidLoad()
viewBox.center = CGPoint(x:0, y: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Without the Line
cntIntViewBox.text=String(location.x)
it works great. Touching on the sceen, the viewBox jumps, starting Touching, the box is moving.
But if I activate the line to show the coordinate (for example, I mean any dynamic text), moving and jumping is not working anymore.
why does this issue arise?
The problem is that your line changes the text of a label. This causes Auto Layout to be performed throughout your interface. Your viewBox view is positioned by Auto Layout constraints, so those constraints take over and are used to position the view. The constraints didn't change so the view doesn't move.
I'm trying to get the coordinates of a touch on the screen, but every time it just gives me 0, 0. I could only find basic help with this function in the new version of Swift, and nothing on this problem. What am I doing wrong here?
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInView(self.view)
NSLog("location: %d, %d", location.x, location.y)
}
Thanks!
As per swift 1.2
override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
if let touch = touches.first as? UITouch {
let location = touch.locationInView(self.view)
println("Location: \(location)")
}
}
As per swift 1.1
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as? UITouch
let location = touch?.locationInView(self.view)
println("Location: \(location)")
}
Tested.
My situation is that i have two views, one view -> MoveView should be movable inside of the other view -> FrameView. But when moving the inner view, it can be moved outside the FrameView!? I hope all the Views didn't disturbed you ;D!
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var MoveViewLocation = CGPoint()
super.touchesBegan(touches , withEvent:event)
if let touch = touches.first as? UITouch {
MoveViewLocation = touch.locationInView(FrameView)
MoveView.center = MoveViewLocation
}
}
I am having trouble creating a function "touchesBegan" and then creating a UIPoint and UITouch constant or variable that holds an x and y coordinate. I have the exact code I want in Objective-C but I do not know what it's equivalent is in Swift. Here is the Objective-C code which I want to basically translate into Swift code...
NOTE: This is a Single View Application, NOT a game... Thanks in advance.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if (point.x < 160) {
var = 10;
}
else{
var = 20;
}
}
As of Swift 1.2, use this
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
var touch = touches.first as! UITouch
var point = touch.locationInView(self)
if point.x < 160 {
var = 10;
}
else{
var = 20;
}
}
Where's the problem?
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
if point.x < 160 {
var variableName = 10;
}
else{
var variableName = 20;
}
Swift 1.2 changed the syntax for touchesBegan. See the UIResponder Reference.
func touchesBegan(_ touches: Set<NSObject>, withEvent event: UIEvent)
And don't forget to reference the super.
super.touchesBegan(touches, withEvent: event)
Here is an edited example of implementing this code from techotopia.com which might give you more information on the other three touches functions.
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
// Insert if statements
super.touchesBegan(touches, withEvent: event)
}