Wie speichere ich den touchBegan-Wert. Ich benutze UItouch, um 5 UIImageViews zu verschieben und wenn du imageView an der richtigen Stelle platzierst bleibt das imageView in der richtigen Position. Aber wenn Sie es nicht an der richtigen Position platzieren, sollte das Bildfenster zurück zur Berührungsstelle springen.So speichern Sie BerührungenBegangen TouchLocation-Werte
Hier einige der Code verwende ich:
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!");
}];
}
}
Im Moment ich imageViewBil1.frame = CGRectMake(44, 692, 308, 171);
mit der Position zu setzen, wenn ich auf der rechten Seite nicht die Imageview setzen Position. Ich möchte die BerührungenBegan Lage haben.
möchte ich so etwas wie dies zu tun: imageViewBil1.frame = (touchesBegan.touchLocation);
oder dieses: imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
Aber wie kann ich das speichern? – user3266053
können Sie den Ursprung im NSPoint-Objekt speichern. – Sommm
Dieser Link wird Ihnen helfen ... http://adampreble.net/blog/2008/01/making-nspointnsrectnssize-objects/ – Sommm