2013-03-15 62 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 
       } 
      } 
    } 
}