如何保存touchesBegan touchLocation值

时间:2016-09-21 09:26:51

标签: ios objective-c uitouch

如何保存touchesBegan值。我使用UItouch移动5个UIImageViews,当你将imageView放在正确的位置时,imageView保持在正确的位置。但如果你没有将它放在正确的位置,imageView应该跳回touchesBegan位置。

以下是我正在使用的一些代码:

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!");
                         }];


        }
    }

目前,当我没有将imageView放在正确位置时,我使用imageViewBil1.frame = CGRectMake(44, 692, 308, 171);来设置位置。我想拥有touchesBegan的位置。

我想做这样的事情:imageViewBil1.frame = (touchesBegan.touchLocation);

或者:imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];

5 个答案:

答案 0 :(得分:2)

如何将图像移动到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);

}

答案 1 :(得分:1)

首先,您必须保存所选图像的原点,然后在拖动/移动后保存该位置的原点。 现在您同时拥有原点(初始位置原点和目标位置原点)。 现在你可以做一些实验。

答案 2 :(得分:1)

你有

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

将位置保存在数组中,即

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
}

答案 3 :(得分:1)

您绝对可以在touchesBegan方法中将最后一帧值保存在数组中。并且考虑第一个索引将保存imageViewBil1的帧,第二个索引将保存imageViewBil2的相同内容,依此类推。

假设数组为originalFrames

代码就是:

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

但我不赞同这种做法,理想的做法是创建一个UIImageView的子类,并附加属性originalFrame。 每个imageViewBill都应该是新子类的实例。由于它们都有新属性originalFrame,您可以在touchesBegan中为其指定值。并在touchesEnded方法中使用此属性将视图置于其原始位置。

我们说子类是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;
     }
}

这样你的touchesBegan保持简短而甜蜜。代码结构为您提供了更高的可读性,健壮性和可伸缩性。

答案 4 :(得分:1)

<强>解决!

比我想象的容易!

在.h档案中,我刚刚添加了NSMutableArray *indexArray;

然后我可以在imageViewBil1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue]; else语句中使用touchesEnded,而不是imageViewBil1.frame = CGRectMake(44, 692, 308, 171);