我有一台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..。ArrayImages2的插座与UIImageViews的插座相同吗?
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;
}我必须知道触摸是否在两个可能的UIImageViews中的一个中,如果是,就移动它。Contador1和Contador2 int是计数有多少UIImageView可见的计数器,用户只能移动其中的最后2个。
它起作用了,问题是当我在外面触摸的时候,它会使应用程序崩溃。如果我在touchesBegan和TouchesMoved中更改索引"posible = [ArrayImage..",对于0,它只对第一个UIImageView有效(我理解为什么),但它不会崩溃。
有什么想法吗?
发布于 2012-07-28 05:35:14
我必须知道触摸是否是在两个可能的UIImageViews中的一个
我不能很好地理解你的代码,但是如果你首先检查点是否在视图的矩形内,你似乎可以简化事情。然后把你的逻辑建立在结果的基础上。就像这样
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
}https://stackoverflow.com/questions/11695595
复制相似问题