首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS touchesBegan问题

iOS touchesBegan问题
EN

Stack Overflow用户
提问于 2012-07-28 05:01:04
回答 1查看 416关注 0票数 0

我有一台15 UIImageViews的NSArray:

代码语言:javascript
复制
@interface ViewController : UIViewController{

    NSArray *ArrayImages1;
    NSArray *ArrayImages2;

}

在viewDidLoad中:

代码语言:javascript
复制
 ArrayImages1 = [[NSArray alloc] initWithObjects: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, nil];

其中a1,a2..。ArrayImages2的插座与UIImageViews的插座相同吗?

TouchesBegan和TouchesMoved:

代码语言:javascript
复制
-(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;
}

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

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

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-28 05:35:14

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

我不能很好地理解你的代码,但是如果你首先检查点是否在视图的矩形内,你似乎可以简化事情。然后把你的逻辑建立在结果的基础上。就像这样

使用CGRectContainsPoint

代码语言:javascript
复制
bool CGRectContainsPoint (
   CGRect rect,
   CGPoint point
);



UIImageView *foundView

for(UIImageView* theView in ArrayImages1){

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

}

然后..。

代码语言:javascript
复制
if (foundView != nil){
// do some logical thing

}

或者..。

代码语言:javascript
复制
if ([foundView isEqual:someOtherView]){
// I may have the syntax wrong on the above

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11695595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档