2013-03-15 63 views
-5

在我的代码中,我在数组中创建了点,并使用该数组创建了10个按钮。我按顺序将每个按钮的标记设置为0 - 9。我想要做的是检查按钮何时滑过(而不是轻敲),然后能够检查它的标签并根据标签做些什么!检查按钮是否已滑过,并检查其标签? (iOS)

这里是我的代码:

#import "LevelOneController.h" 

@interface LevelOneController() 

@end 

@implementation LevelOneController 
@synthesize whereStuffActuallyHappens, squareLocations; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"View loaded"); 

    squareLocations = [[NSMutableArray alloc] init]; 

    CGPoint dotOne = CGPointMake(1, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotOne]]; 

    CGPoint dotTwo = CGPointMake(23, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotTwo]]; 

    CGPoint dotThree = CGPointMake(45, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotThree]]; 

    CGPoint dotFour = CGPointMake(67, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotFour]]; 

    CGPoint dotFive = CGPointMake(89, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotFive]]; 

    CGPoint dotSix = CGPointMake(111, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotSix]]; 

    CGPoint dotSeven = CGPointMake(133, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotSeven]]; 

    CGPoint dotEight = CGPointMake(155, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotEight]]; 

    CGPoint dotNine = CGPointMake(177, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotNine]]; 

    CGPoint dotTen = CGPointMake(199, 25); 
    [squareLocations addObject:[NSValue valueWithCGPoint:dotTen]]; 

    int numby = [squareLocations count]; 

    for (int i = 0; i < numby; i++) 
    { 
     NSValue *pointLocation = [squareLocations objectAtIndex:i]; 
     CGPoint tmpPoint = [pointLocation CGPointValue]; 
     UIImage *theSquare = [UIImage imageNamed:@"square.png"]; 

     UIButton *squareButton = [[UIButton alloc] init]; 

     squareButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     squareButton.frame = CGRectMake(tmpPoint.x, tmpPoint.y, theSquare.size.width, theSquare.size.height); 
     squareButton.tag = *(&i); 
     [squareButton setImage:theSquare forState:UIControlStateNormal]; 

     [whereStuffActuallyHappens addSubview:squareButton]; 


    } 
} 

有谁知道的一个很好的方式来实现这一目标?谢谢!

+0

对不起,我新我不知道事情怎么在这里运行 – 2013-03-15 20:04:13

回答

0

我想你可以做这样的事情,不知道怎么会是高性能不过:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    for (UITouch *touch in touches) { 
      CGPoint locationInView = [touch locationInView:touch.view]; 
      for (NSValue *pointValue in squareLocations) { 
       CGPoint point = [pointValue CGPointValue]; 
       if (CGRectContainsPoint(CGRectMake(point.x, point.y, 50, 50),locationInView) == YES) { 
         //This button or location has been dragged over 
       } 
      } 
    } 
}