gpt4 book ai didi

objective-c - 错误消息 : "index 1 beyond bounds"

转载 作者:行者123 更新时间:2023-12-01 17:45:50 25 4
gpt4 key购买 nike

我只是一个菜鸟,我似乎找不到错误。这是一段用于缩放、旋转和移动 UIImageView 的代码。我可以移动 UIImageView 但是当我尝试旋转或缩放 UIImageView 时它会崩溃。我确实将 setMultipleTouchEnabled 设置为 YES。

崩溃是因为:[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]
我认为错误在于文本的这一部分:

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

NSArray *allTouches = [touches allObjects];

UITouch* t;
if([[event allTouches] count]==1){
if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {
t=[[[event allTouches] allObjects] objectAtIndex:0];
touch1=[t locationInView:nil];
}
}else{
t=[[[event allTouches] allObjects] objectAtIndex:0];
touch1=[t locationInView:nil];
t=[[[event allTouches] allObjects] objectAtIndex:1];
touch2=[t locationInView:nil];
}
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
double deltaX, deltaY;
deltaX = point1.x - point2.x;
deltaY = point1.y - point2.y;
return sqrt(deltaX * deltaX + deltaY * deltaY);
}

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

CGPoint currentTouch1;
CGPoint currentTouch2;
NSArray *allTouches = [touches allObjects];
UITouch* t;
float scale,rotation;

if([[event allTouches] count]==1){
t=[[[event allTouches] allObjects] objectAtIndex:0];
if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]))
{
touch2=[t locationInView:nil];
Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
touch1=touch2;
}
}
else if([[event allTouches] count]==2)
{
t=[[[event allTouches] allObjects] objectAtIndex:0];
currentTouch1=[t locationInView:nil];

t=[[[event allTouches] allObjects] objectAtIndex:1];
currentTouch2=[t locationInView:nil];

double distance1 = [self distance:currentTouch1 toPoint:currentTouch2];
double distance2 = [self distance:touch1 toPoint:touch2];

if (distance2 == 0)
{
//handle the case where distance is zero
}
else {
scale =distance1 / distance2;}

rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
if(isnan(scale)){
scale=1.0f;
}
NSLog(@"rotation %f",rotation);

NSLog(@"scale %f",scale);

if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]) &&
CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:1] locationInView:self.view]))
{

Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);
}
else // In case of scaling or rotating the background imageView
{
imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
}

touch1=currentTouch1;
touch2=currentTouch2;
}
}

提前致谢!

最佳答案

当您调用此电话时:

  if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:self.view]) &&
CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:1] locationInView:self.view]))

确保在 allTouches 中确实有两个触摸对象。

关于objective-c - 错误消息 : "index 1 beyond bounds",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5720885/

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