How do I save the touchesBegan value. I am using UItouch to move 5 UIImageViews and when you place the imageView at the right place the imageView stays in the right position. But if you don't place it in the right position the imageView should jump back to the touchesBegan position.
Here is some of the code I am using:
ViewDidLoad:
[super viewDidLoad];
// Do any additional setup after loading the view.
//Create an array with the rectangles used for the frames
NSMutableArray *indexArray = [NSMutableArray arrayWithObjects:
[NSValue valueWithCGRect:CGRectMake(44, 692, 308, 171)],
[NSValue valueWithCGRect:CGRectMake(31, 835, 308, 171)],
[NSValue valueWithCGRect:CGRectMake(433, 681, 308, 171)],
[NSValue valueWithCGRect:CGRectMake(258, 774, 308, 171)],
[NSValue valueWithCGRect:CGRectMake(411, 828, 308, 171)],
nil];
//Randomize the array
NSUInteger count = [indexArray count];
for (NSUInteger i = 0; i < count; ++i) {
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[indexArray exchangeObjectAtIndex:i withObjectAtIndex:n];
for (int x = 0; x < [indexArray count]; x++) {
int randInt = (arc4random() % ([indexArray count] - x)) + x;
[indexArray exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
}
//Assign the frames
imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
imageViewBil2.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue];
imageViewBil3.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue];
imageViewBil4.frame = [((NSValue *)[indexArray objectAtIndex:3]) CGRectValue];
imageViewBil5.frame = [((NSValue *)[indexArray objectAtIndex:4]) CGRectValue];
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageViewBil1) { // If touched view is imageViewBil1 , then assign it its new location
//imageViewBil1.center = touchLocation;
[self.view bringSubviewToFront:imageViewBil1];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//NSLog(@"%@", NSStringFromCGPoint(touchLocation));
if ([touch view] == imageViewBil1 && imageViewBil1.tag == 0) {
//imageViewBil1.tag=0;
if ((touchLocation.x >= 190 && touchLocation.x <= 260) && (touchLocation.y >= 90 && touchLocation.y <= 155)) {
NSLog(@"bil 1");
[UIView animateWithDuration:0.35
delay:0.0
options: UIViewAnimationOptionAllowAnimatedContent
animations:^{
imageViewBil1.frame = CGRectMake(44, 30, 308, 171);
imageViewBil1.tag=1;
NSURL *musicFile;
musicFile = [NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:@"tuta"
ofType:@"mp3"]];
myAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[myAudio play];
myAudio.delegate = self;
}
completion:^(BOOL finished){
NSLog(@"Auto justerad!");
}];
}
else {
[UIView animateWithDuration:0.35
delay:0.0
options: UIViewAnimationOptionAllowAnimatedContent
animations:^{
imageViewBil1.frame = CGRectMake(44, 692, 308, 171);
}
completion:^(BOOL finished){
NSLog(@"Hoppar tillbaka!");
}];
}
}
For the moment I using imageViewBil1.frame = CGRectMake(44, 692, 308, 171);
to set the position when i don't put the imageView on the right position. I want to have the touchesBegan location.
I want to do something like this: imageViewBil1.frame = (touchesBegan.touchLocation);
or this: imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];