响应UIControlEventTouchUpInside并触及方法的按钮

时间:2012-02-29 14:06:12

标签: objective-c ios cocoa-touch

我创建了一个按钮:

button = [StretchableButton new];
[button addTarget:mainViewController action:@selector(addNumberFromButton:) forControlEvents:UIControlEventTouchUpInside];

我在StretchableButton课程内:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
    [self.nextResponder touchesMoved:touches withEvent:event]; 
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    [self.nextResponder touchesEnded:touches withEvent:event]; 
}

调用触控方法,但button不会对点击做出反应。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

添加手势识别器,代码示例:

    UIPanGestureRecognizer *gesturePan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    gesturePan.maximumNumberOfTouches = 1;
    gesturePan.minimumNumberOfTouches = 1;
    gesturePan.delegate = self;
    [myButton addGestureRecognizer:gesturePan];
    [gesturePan release];

...

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {


    CGPoint location = [gesture locationInView:[gesture.view superView]];
//add calculation view location on it superview
    [gesture.view superView] setCenter:newLocation];

}