突出显示手指下的标签

时间:2012-09-07 00:17:32

标签: ios cocoa-touch

初始化

假设我有5个标签,标记为1-5。这5个标签附加到使用

初始化的IBOutletCollection
 @property (nonatomic, retain) IBOutletCollection(UILabel) NSMutableSet* myLineCollection;

我想做什么:

当我的手指触摸并在屏幕上移动时,我希望在myLineCollection中接收我的手指所在的标签。

我想收到这个,因为我的最终目标是改变手指当前从红色到蓝色的标签颜色。但当我的手指移开时,它应该恢复原来的颜色,蓝色。

我在想我应该使用

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

和/或

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

和/或

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

我一直在尝试:

如果触摸位于label.window中,则使用touches和if语句,如果触摸点远离label.frame.center,则触发点为label.frame.length。

我如何收到当前正在触摸的标签,然后对该标签执行某些操作。 这就是我所需要的一切

1 个答案:

答案 0 :(得分:2)

希望这会对你有所帮助:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    //here loops all labels
    for(){
        if (CGRectContainsPoint([lable frame],touchPoint)) {
            //change label's background color
        }
    }
}