Cocoa Touch - UIButtons - 对UIButton进行子类化

时间:2010-09-24 23:03:19

标签: xcode uibutton subclass drag

嘿所有人,有没有人可以解释我如何子类化UIButton并覆盖某些方法,以便当用户拖动按钮时立即出现?问题是,当我拖出按钮框架时,它会保持活动状态并向下移动。我希望它一旦手指离开按钮框就停止。有什么想法吗?

(Cocoa Touch)

1 个答案:

答案 0 :(得分:4)

如果有人遇到此问题,以下代码允许在拖动时进行极其准确的边缘感应。如果你拖出按钮,它就不会像正常一样延伸过按钮的边缘。

(我将UIButton子类化并做了以下内容:)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touchBlocker = TRUE;
    self.highlighted = TRUE;
    NSLog(@"Touch Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    if (touchBlocker) {
        if (!CGRectContainsPoint([self bounds], location)) {
            touchBlocker =FALSE;
            self.highlighted = FALSE;
            NSLog(@"Touch Exit");
        }   
    } else if (CGRectContainsPoint([self bounds], location)) {
        touchBlocker = TRUE;
        self.highlighted = TRUE;
        NSLog(@"Touch Enter");
    }

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    touchBlocker = FALSE;
    self.highlighted = FALSE;
    NSLog(@"Touch Ended");
}