gpt4 book ai didi

ios - 如何保存 touchesBegan touchLocation 值

转载 作者:可可西里 更新时间:2023-11-01 17:14:13 26 4
gpt4 key购买 nike

如何保存 touchesBegan 值。我正在使用 UItouch 移动 5 个 UIImageView,当您将 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!");
}];


}
}

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

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

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

最佳答案

如何在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);

}

关于ios - 如何保存 touchesBegan touchLocation 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com