0

我有一个带有 15 个 UIImageViews 的 NSArray:

@interface ViewController : UIViewController{

    NSArray *ArrayImages1;
    NSArray *ArrayImages2;

}

在 viewDidLoad 中:

 ArrayImages1 = [[NSArray alloc] initWithObjects: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, nil];

其中 a1,a2... 是 UIImageViews 的出口,对于 ArrayImages2 也是如此

TouchesBegan 和 TouchesMoved:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    UIImageView *posible;
    int imagen;
    if (point.x < 161) {
        imagen = Contador1;
        if (Contador1 > 14) {imagen = Contador1 - 15;}
        imagen--;
        posible = [ArrayImages1 objectAtIndex:imagen];
    }
    else if (point.x > 159) {
        imagen = Contador2;
        if (Contador2 > 14) {imagen = Contador2 - 15;}
        imagen--;
        posible = [ArrayImages2 objectAtIndex:imagen];
    }
    if ([touch view] != posible) { return; }
    original2 = posible.center;    
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    UIImageView *posible;
    int imagen;
    if (point.x < 161) {
        imagen = Contador1;
        if (Contador1 > 14) {imagen = Contador1 - 15;}
        imagen--;
        posible = [ArrayImages1 objectAtIndex:imagen];
    }
    else if (point.x > 159) {
        imagen = Contador2;
        if (Contador2 > 14) {imagen = Contador2 - 15;}
        imagen--;
        posible = [ArrayImages2 objectAtIndex:imagen];
    }
    if ([touch view] != posible) { return; }
    posible.center = point;
}

我必须知道触摸是否在两个可能的 UIImageView 之一中,如果是,请移动它。Contador1 和 Contador2 整数是计算有多少 UIImageView 可见的计数器,用户只能移动其中的最后 2 个。

它有效,问题是当我触摸外面时,它会使应用程序崩溃。如果我在 touchesBegan 和 TouchesMoved 中更改,“posible = [ArrayImage..”的索引为 0,它只适用于第一个 UIImageView(我明白为什么),但它不会崩溃。

有任何想法吗?

4

1 回答 1

0

我必须知道触摸是否在两个可能的 UIImageView 之一中

我不能很好地遵循您的代码,但如果您首先检查该点是否在视图的矩形内,您似乎可以简化事情。然后根据结果建立你的逻辑。就像是

使用CGRectContainsPoint

bool CGRectContainsPoint (
   CGRect rect,
   CGPoint point
);



UIImageView *foundView

for(UIImageView* theView in ArrayImages1){

 if (CGRectContainsPoint(theView.frame, touchPoint){
     foundView = theView;
     break;
 }

}

然后...

if (foundView != nil){
// do some logical thing

}

或者...

if ([foundView isEqual:someOtherView]){
// I may have the syntax wrong on the above

}
于 2012-07-27T21:35:13.587 回答