【问题标题】:iOS touchesBegan issueiOS touchesBegan 问题
【发布时间】:2012-07-27 21:01:03
【问题描述】:

我有一个带有 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 int 是计算有多少 UIImageView 可见的计数器,用户只能移动其中的最后 2 个。

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

有什么想法吗?

【问题讨论】:

    标签: ios touchesbegan touchesmoved


    【解决方案1】:

    我必须知道触摸是否在两个可能的 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
    
    }
    

    【讨论】:

    • 感谢您的快速回复!看,只需要验证触摸点是否在 ArrayImages1(一个来自这里)和 ArrayImages2(另一个来自这里)的最后两个 UIImageView 之一中,因为我制作了“可能的”UIImageView(指向两个数组中的最后一个,并检查用户是否触摸了一个)。我认为我不必搜索数组的所有元素,只需在最后一个可见的元素中搜索(我知道它的 Contador1-1)
    • 您发布的代码中的逻辑很难理解,所以我无法猜测。简化这一点,您可以使用数组函数 [array lastObject] 获取数组中的最后一个对象。至于崩溃,我想我会检查 'posible' 是否为 nil,并检查崩溃日志/错误以查看是否可以查明原因。
    • 我解决了制作两个 NSMutableArray 并使用 lastObject 方法的问题,感谢 skinnyTOD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多