0

My Question is how can i change the size of a button while the finger is hovering over it?

Sorry if this Question is already answered but i didn't found any results for me. Please link the Thread as an answer if its already answered :)

I already tried it with a UIView and then listening with a CGRectIntersectsRect if it touches the button. But i think theres a simpler way of doing this.

4

3 回答 3

1

您可以添加一个观察该事件的方法。我认为您正在寻找的是 UIControlEventTouchDown

- (void)initOrSomething {
    UIButton *button = ...
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
}

- (void)buttonPress:(UIButton *)button {
    button.frame = CGRectMake(0, 0, 100, 100);
}

如果您将自己添加为目标,您应该能够更改选择器中的框架。

于 2013-07-23T16:21:58.317 回答
1
- (IBAction)buttonPressed:(id)sender {
    CGRect f = self.button2.frame;
    f.origin.x += 10;
    f.size.width += 10; 
    self.button2.frame = f;
}

Add this action with UIControlEventTouchDown.

Hope this helps...

于 2013-07-23T16:23:21.043 回答
-1
- (void)viewDidLoad {
    ...
    UILongPressGestureRecognizer *pressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed:)];
    pressGesture.minimumPressDuration = 0.0;
    pressGesture.delegate = self;
    [_button addGestureRecognizer: pressGesture];
    ...
}


- (void)buttonPressed:(UILongPressGestureRecognizer *)rec {
    switch (rec.state) {
        case UIGestureRecognizerStateBegan:
            [UIView animateWithDuration:0.1 animations: ^{
                 _button.transform = CGAffineTransformMakeScale(0.8, 0.8);
             }];
             break;
        case UIGestureRecognizerStateEnded: 
            [UIView animateWithDuration:0.1 animations: ^{
                 _button.transform = CGAffineTransformIdentity;
             }];
             break;
        default: 
             break;
    }

}

这基本上使按钮在用户按住按钮时收缩。当手指抬起时,按钮大小会恢复到原始大小。

于 2018-04-09T21:15:40.877 回答