Why won't my ImageView move? - ios

All in all I am trying to get a result when one image touches another image. That is not the problem I am having though. I can't get the UIImageView to even move when I click on it and drag. Below is the code I am using! Please let me know if you can help me! Thanks!
.h
{
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
}
#property (nonatomic, retain) UIImageView *image1;
#property (nonatomic, retain) UIImageView *image2;
-(void)collision;
.m
#synthesize image1, image2;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch1 = [touches anyObject];
[self collision];
if ([touch1 view] != self.image1) {
return;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
// If the touch was in the placardView, move the placardView to its location.
if ([touch1 view] == self.image1) {
CGPoint location = [touch1 locationInView:image1];
self.image1.center = location;
return;
}
}
-(void)collision
{
if (CGRectIntersectsRect(image1.frame, image2.frame)) {
NSLog(#"Touched");
}
}
** I was able to get the code to work where I am able to move the UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [[event allTouches] anyObject];
image1.center = [touch1 locationInView:self.view];
return;
}
The only problem with this is that where ever I touch on the screen the image moves to that location. How may I have it where you have to touch the actual UIImageView to move it? Thanks!!

UIImageView default userInteraction is false, so u are just enable userInteraction is True for
both image view.

Related

Touched Move UILabel works until the value is fix - Bug or Features?

a very strange result.
Start a Single View Application
Add a UILabel
Put my Code in
import "ViewController.h"
#interface ViewController ()
{
float oldX, oldY;
bool dragging;
__weak IBOutlet UILabel *textLable;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// self.textLable.backgroundColor = [UIColor clearColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textLable)
{
int intValue = (int)((touchLocation.y * 24.0)/300.0);
NSString *anyValue = [NSString stringWithFormat:#"%d",intValue];
textLable.center= touchLocation;
// RUN WITHOUT THIS PART .... tochedMoved workt great
//
// Run With this PART ..... touchedMoved is damaged!!!!!
//textLable.text = anyValue;
NSLog(#"%f %f", touchLocation.y, textLable.frame.origin.y);
}
}
#end
Connect label with
__weak IBOutlet UILabel *textLable;
let it RUN
Now you can move the label by touching moving.
Ok...and NOW!!!!
change the change
// RUN WITHOUT THIS PART .... tochedMoved workt great
//
// Run With this PART ..... touchedMoved is damaged!!!!!
//textLable.text = anyValue;
to
// RUN WITHOUT THIS PART .... tochedMoved workt great
//
// Run With this PART ..... touchedMoved is damaged!!!!!
textLable.text = anyValue; //<------------------------------
Run the app and try the move!!! you will see, label jumps between new and start position, if you start touched moving.
I tried by using a UIView as container....same thing: Once you change the value of moved object (UIButton same), moving is not working right.
Report to Appel is already send...no answere!!!
Is it a Bug or a features???
The idea is about your frame and your location touch. You are centering Label to your touch. So nothing is about setting the text.
Try this code, detecting distance between touch and centre text when first touch and adding the center label to this point :
#interface ViewController ()
{
float distanceTouchX, distanceTouchY;
bool dragging;
CGPoint firstLocation;
__weak IBOutlet UILabel *textLable;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
distanceTouchX = textLable.center.x - touchLocation.x;
distanceTouchY = textLable.center.y - touchLocation.y;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textLable)
{
int intValue = (int)((touchLocation.y * 24.0)/320.0);
NSString *anyValue = [NSString stringWithFormat:#"%d",intValue];
CGPoint newTouchLocation = touchLocation;
newTouchLocation.x = newTouchLocation.x + distanceTouchX;
newTouchLocation.y = newTouchLocation.y + distanceTouchY;
textLable.center= newTouchLocation;
textLable.text = anyValue;
NSLog(#"%f %f", touchLocation.y, textLable.frame.origin.y);
}
}
#end

How to make create image when screen is touched then be able to move it

I am trying to create a dot on the screen when it is touched and then be able to move that dot around and then ending the touch which will bring up an alert view. For the first touch and movement i have this code so far...
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
dot.center = location;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesBegan:touches withEvent:event];
}
The problem is that this creates a new object when moving instead of just when I first touch it. I understand why it does this, but i don't know any way to fix it. Could someone please help? Thanks!
Create a property in your .h file:
#property (nonatomic) UIImageView *dot;
And then just create the view if it doesn't exist and then move the property:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.dot) {
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(359,487,50,50)];
dot.image=[UIImage imageNamed:#"hockey-puck-stress-toy-superextralarge-175648.png"];
[self.view addSubview:dot];
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
self.dot.center = location;
}

Having trouble getting a touch location in sprite-kit screne

I am making a game for a final project and I need to know where the user touches so I can move the player around.
here is the .h file
#import <SpriteKit/SpriteKit.h>
#interface BYFGameScene : SKScene
#end
the header of the .m
#import "BYFGameScene.h"
#interface BYFGameScene()
#property BOOL contentCreated;
#property (nonatomic)SKSpriteNode *player;
#end
#implementation BYFGameScene
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint *location = [[touches anyObject] location];
NSLog(#"the location is %#",location);
}
I think that touches began would be the appropriate method to get the location and in previous apps this is how I would get the location but it keeps throwing a exception and I am not sure why.
I have also tried
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat location = [[touches anyObject] locationInView:self];
NSLog(#"the location is %f",location);
}
but it gives an error saying that UIView and BYFGameScene(the current scene) are not compatible types
also how would I manipulate the players position?
\here is what I thought would work but it does not.
-(void)moveUp
{
if(self.player.position==CGpoint 700)
{
}
else
{
self.player.position = self.player.position +100;
}
}
700 would be the max position
Any help would be appreciated
thanks
I think this is what you're looking for:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//touch point, see locationInNode
CGPoint location = [touch locationInNode:self];
// SKNode *node = [self nodeAtPoint:location];

IOS touch tracking code

I'm making an app for iPhone in Xcode, and It requires a box to follow my finger only on the X axis. I couldn't find any solution online to this, and my coding knowledge isn't that great.
I've been trying to use touchesBegan and touchesMoved.
Could someone please write me up some code please?
First you need the UIGestureRecognizerDelegate on your ViewController.h file:
#interface ViewController : UIViewController <UIGestureRecognizerDelegate>
#end
Then you declare an UIImageView on your ViewController.m, like so, with a BOOL to track if a touch event is occurring inside your UIImageView:
#interface ViewController () {
UIImageView *ballImage;
BOOL touchStarted;
}
Then you initialize the UIImageView on viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"ball.png"];
ballImage = [[UIImageView alloc]initWithImage:image];
[ballImage setFrame:CGRectMake(self.view.center.x, self.view.center.y, ballImage.frame.size.width, ballImage.frame.size.height)];
[self.view addSubview:ballImage];
}
After that you can start doing your modifications to what's best for you using these methods:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touch_point = [touch locationInView:ballImage];
if ([ballImage pointInside:touch_point withEvent:event])
{
touchStarted = YES;
} else {
touchStarted = NO;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count]==1 && touchStarted) {
UITouch *touch = [touches anyObject];
CGPoint p0 = [touch previousLocationInView:ballImage];
CGPoint p1 = [touch locationInView:ballImage];
CGPoint center = ballImage.center;
center.x += p1.x - p0.x;
// if you need to move only on the x axis
// comment the following line:
center.y += p1.y - p0.y;
ballImage.center = center;
NSLog(#"moving UIImageView...");
}
}

how to stop my object to skip by clicking and move just by touch

I have some object on my screen view which moves by touch, but the problem is if I click somewhere else on my screen which has no object on, the last moved object skipped to that clicked position, anyone who know how I can be able to stop that? PostView contains code about my objects
in .h file
#property (weak, nonatomic) IBOutlet PostView *pv;
and .m file
- (void)viewDidLoad
{
[super viewDidLoad];
viewArray = [NSMutableArray arrayWithObjects: nil];
pv.frame = CGRectMake(10, 10, 100, 100);
pv.backgroundColor = [UIColor blackColor];
[viewArray insertObject:pv atIndex:0];
[self.view addSubview:pv];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint firstTouch = [touch locationInView:self.view];
for (PostView *view in viewArray) {
if (CGRectContainsPoint(view.frame, firstTouch)) {
toMove = view;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
toMove.center = location;
// [_delegate dragViewDidMove:self];
// toMove.center = currentTouch;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.view];
toMove.center = currentTouch;
}
Set toView to null at the start of touchesBegan:. This ensures that if the user makes a touch that touches nothing (and so toView doesn't get set to anything deliberately), nothing will happen when the touch moves (the setCenter: message will be sent to null which will return null and have no side effects.)

Resources