2016-09-21 2 views
3

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];

Antwort

1

zuerst müssen Sie den Ursprung des Bildes speichern hatten Sie ausgewählt und dann nach Drag/verschieben den Ursprung dieser Stelle speichern. Jetzt haben Sie sowohl den Ursprung (Ursprungsort und Herkunftsort). Jetzt können Sie experimentieren.

+0

Aber wie kann ich das speichern? – user3266053

+0

können Sie den Ursprung im NSPoint-Objekt speichern. – Sommm

+0

Dieser Link wird Ihnen helfen ... http://adampreble.net/blog/2008/01/making-nspointnsrectnssize-objects/ – Sommm

2

wie ein Bild an einem bestimmten Punkt in touchesBegan bewegen:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 

CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 

imageViewBil1.frame = CGRectMake(touchPoint.x, touchPoint.y, imageViewBil1.frame.size.width, imageViewBil1.frame.size.height); 

} 
1

Sie haben

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
} 

Speichern Sie die Lage in einem Array, das

NSValue *valueToStore = [NSValue valueWithCGPoint:location]; 
NSMutableArray *locationArray =[NSMutableArray arrayWithObject:valueToStore]; 


CGPoint locArr; 
for (NSValue *valuetoGetBack in locationArray) { 
    locArr = [locationArray CGPointValue]; 
    //do something with the CGPoint 
} 
+0

Wie speichere ich den Standort in einem Array? – user3266053

+0

[IhrArray addObject: [NSValue valueWithCGPoint: location]]; – gurmandeep

+0

Hinzugefügt in meiner Antwort – gurmandeep

1

Sie tun kann definitiv die letzten Frame-Werte in einem Array speichern, in touchesBegan Methode. Und bedenken Sie, 1. Index wird Frame für imageViewBil1 speichern, 2. Index wird das gleiche für imageViewBil2 und so weiter speichern.

Lasst sie sagen Array originalFrames

-Code ist für das sein würde:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 

if ([touch view] == imageViewBil1) {  
    [self.view bringSubviewToFront:imageViewBil1]; 
    NSValue *originalFrame = [NSValue valueWithCGRect:imageViewBil1.frame]; 
    originalFrames[0] = originalFrame 
} else if { 
// same for other views 
} 

Aber ich würde eine Unterklasse von UIImageView zu schaffen, mit zusätzlicher Eigenschaft nicht billigen wäre, diese Praxis idealen Weg, dies zu tun originalFrame. Jede imageViewBills sollte Instanzen der neuen Unterklasse sein. Und da sie alle eine neue Eigenschaft originalFrame haben, können Sie in touchesBegan Wert für es zuweisen. Und verwenden Sie diese Eigenschaft in touchesEnded Methode, um die Ansicht an die ursprüngliche Position zu platzieren.

Sagen wir, Unterklasse ist BillsImageView.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 

    if ([[touch view] isKindOfClass:[BillsImageView class]]) {  
     ((BillsImageView*)[touch view]).originalFrame = [touch view].frame; 
    } 
} 

Auf diese Weise Ihre touchesBegan Aufenthalte kurz und süß. Und die Code-Struktur bietet Ihnen weit mehr Lesbarkeit, Robustheit und Skalierbarkeit.

+0

Hallo Gurdeep, Tank Sie für Ihre Antwort.Ich habe versucht, Ihren ersten Code zu verwenden, aber ich bekomme die Fehlermeldung auf Zeile 'originalFrame [0] = originalFrame; '" Expected method to write array element nicht gefunden auf Objekt vom Typ 'NSValue *'. Was mache ich falsch? Vielen Dank für Ihre Hilfe. – user3266053

1

Gelöst!

Es war einfacher als ich dachte!

In der .h-Datei habe ich gerade NSMutableArray *indexArray; hinzugefügt.

Dann könnte ich imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue]; in touchesEnded else Anweisung anstelle von imageViewBil1.frame = CGRectMake(44, 692, 308, 171); verwenden.