2012-07-27 71 views
0

我有一个NSArray丝毫15周的UIImageViews:的iOS的touchesBegan问题

@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(我明白为什么),但它确实崩溃。

任何想法?

回答

0

我必须知道如果触摸是两种可能的UIImageViews

我不能按照你的代码非常出色的一个,但它好像你可能会简化事情,如果你先检查点在一个视图的内部。然后根据结果建立逻辑。喜欢的东西

使用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 

} 
+0

感谢QUIK响应!看,只需要验证接触点是否在ArrayImages1(这里是一个)和ArrayImages2(另一个来自这里)的最后两个UIImageView中的一个,因为我制作了“posible”UIImageView(指向最后一个阵列并检查用户是否触摸了一个)。我不认为我必须搜索阵列的所有元素,只是在最后一个可见(我知道它的Contador1-1) – 2012-07-27 23:53:56

+0

您发布的代码中的逻辑很难遵循,所以我不能冒险猜测。简化一下,你可以通过使用数组函数[array lastObject]来获得数组中的最后一个对象。至于碰撞,我想我会检查'posible'是否为零,并检查崩溃记录/错误以查看是否可以确定原因。 – 2012-07-28 00:04:48

+0

我解决了两个NSMutableArray和使用lastObject方法的问题,谢谢skinnyTOD – 2012-07-28 13:49:10