3

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

user3266053
  • 175
  • 1
  • 1
  • 10

5 Answers5

2

how to move a image to a certain point in touchesBegan:

-(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);

}
ben
  • 900
  • 9
  • 17
1

first you have to save the origin of image you had selected and then after drag/move save the origin of that location. now you have both the origin( initial location origin and destination location origin). Now you can do experiment on it .

Sommm
  • 527
  • 4
  • 22
1

You have

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

Save the location in an Array, that will do

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
}
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
1

You can definitely save the last frame values in an array, in touchesBegan method. And consider 1st index will save frame for imageViewBil1, 2nd index will save the same for imageViewBil2 and so on.

Let's say array is originalFrames

Code for that would be :

-(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
}

But I would not endorse this practice, ideal way of doing this would be create a subclass of UIImageView, with additional property originalFrame. Each of imageViewBills should be instances of the new Subclass. And as they all have a new property originalFrame, You can assign value for it in touchesBegan. And use this property in touchesEnded method to place the view to its original position.

Let's say subclass is 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;
     }
}

In this way your touchesBegan stays short and sweet. And code structure provides you far more readability, robustness and scalability.

Gurdeep
  • 171
  • 9
  • Hello Gurdeep, tank you for your answer. I tried to use your first code but I get the error message on line `originalFrame[0] = originalFrame;` "Expected method to write array element not found on object of type 'NSValue *'. what am I doing wrong? Thank you again for you help. – user3266053 Sep 22 '16 at 08:18
1

SOLVED!

It was easier than I thought!

In the .h file I just added NSMutableArray *indexArray;.

Then I could use imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue]; in touchesEnded else statement, instead of imageViewBil1.frame = CGRectMake(44, 692, 308, 171);.

user3266053
  • 175
  • 1
  • 1
  • 10