检查按钮是不是已滑过,并检查其标签? (iOS)[重复]

Posted

技术标签:

【中文标题】检查按钮是不是已滑过,并检查其标签? (iOS)[重复]【英文标题】:Check if a button has been slid over, and check its tag? (iOS) [duplicate]检查按钮是否已滑过,并检查其标签? (iOS)[重复] 【发布时间】:2013-03-15 19:40:37 【问题描述】:

在我的代码中,我在一个数组中创建了点,并使用该数组创建了 10 个按钮。我将每个按钮的标签按顺序设置为 0 - 9。我想要做的是检查一个按钮何时被滑过(不仅仅是点击),然后能够检查它的标签并根据标签的内容做一些事情!

这是我的代码:

    #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];


    

有没有人知道实现这一目标的好方法?谢谢!

【问题讨论】:

对不起,我是新手,我不知道这里的情况如何 【参考方案1】:

我猜你可以做这样的事情,但不知道它的性能如何:

- (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
                
          
     

【讨论】:

以上是关于检查按钮是不是已滑过,并检查其标签? (iOS)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

IOS/Swift:检查按钮图像以切换按钮图像

检查是不是执行了 MotionEvent.ACTION_MOVE

如何检查按钮形状是不是启用? [复制]

Roblox & C# |检查游戏是不是为FE,并打印到标签

是否可以在 html 中将单选按钮标签助手值属性设置为“已检查”?

检查两个复选框是不是未选中并禁用按钮