如果在UIButton之外触摸,如何更改UIButton的边界?

时间:2015-10-31 03:57:59

标签: ios objective-c uibutton touch border

目前我正在尝试创建一个对象的刻板“选定”动作。也就是说,当我单击对象时,其边框会变为不同的颜色,当我在对象外部单击时,边框会变回其正常颜色。我可以弄清楚当我触摸对象内部时(如果是UIButton)更改对象的边框但是,我不能弄明白如何当我触摸UIButton外部时,将UIButton的边框更改回其原始状态。到目前为止,这是我的代码:

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateBegan ) {
    gesture.view.layer.borderColor = [UIColor lightGrayColor].CGColor;


    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Would you like to delete this rep?"
                                  message:nil
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* deleteButton = [UIAlertAction
                                actionWithTitle:@"Delete"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {



                                        [gesture.view removeFromSuperview];

                                    [alert dismissViewControllerAnimated:YES completion:nil];

                                }];
    UIAlertAction* cancelButton = [UIAlertAction
                               actionWithTitle:@"Cancel"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                                   {

                                       gesture.view.layer.borderColor = [UIColor blackColor].CGColor;

                                   [alert dismissViewControllerAnimated:YES completion:nil];

                               }];

     [alert addAction:deleteButton];
     [alert addAction:cancelButton];

     [self presentViewController:alert animated:YES completion:nil];
     }
     }



 - (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

 {

    panner.view.layer.borderColor = [UIColor lightGrayColor].CGColor;


   _draggedView = panner.view;

    CGPoint offset = [panner translationInView:_draggedView.superview];
    CGPoint center = _draggedView.center;
    _draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
    _buttonField.layer.borderWidth = 4.0f;




    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:_draggedView.superview];

 }

 }



-(void)buttonTouched:(UIButton*)sender forEvent:(id)tap {
NSSet *touches = [tap allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor lightGrayColor].CGColor;
}


-(void)doubleTapped:(UIButton*)sender forEvent:(id)twoTaps {
NSSet *touches = [twoTaps allTouches];
UITouch *touch = [touches anyObject];
UITouchPhase *phase = touch.phase;
touch.view.layer.borderColor = [UIColor blackColor].CGColor;

}




- (IBAction)addRepButton:(UIBarButtonItem *)newRep {

self.labelCounter++;

buttonCount ++;
if (buttonCount > 0 )
{

    _buttonField = [[UIButton alloc]initWithFrame:CGRectMake(300, 300, 28, 28)];
    [_buttonField setTitle:[NSString stringWithFormat:@"%i", self.labelCounter]forState:UIControlStateNormal];
    [_buttonField setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _buttonField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    _buttonField.userInteractionEnabled = YES;
    _buttonField.layer.cornerRadius = 14;
    _buttonField.layer.borderColor = [UIColor blackColor].CGColor;
    _buttonField.layer.borderWidth = 4.0f;
    _buttonField.titleLabel.font = [UIFont systemFontOfSize: 18];
    [_buttonField setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _buttonField.layer.backgroundColor = [UIColor blackColor].CGColor;



    //Pan gesture declared in button
    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWasRecognized:)];
    [_buttonField addGestureRecognizer:panner];

    //Long Press gesture declared in button
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.buttonField addGestureRecognizer:longPress];

    //Touch down inside declared in button
    [self.buttonField addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];

    //Double Tap inside declared in button
    [self.buttonField addTarget:self action:@selector(doubleTapped:forEvent:) forControlEvents:UIControlEventTouchDownRepeat];





    [self.view addSubview:_buttonField];


    }


    }







    @end

我需要了解如何在触摸UIButton外部时将UIButton的边框恢复正常,以获得真正的“选择/取消选择”感觉。

1 个答案:

答案 0 :(得分:1)

似乎问题不在于您无法在用户触摸按钮外触发操作时将边框更改回来。

我建议在用户不接触该按钮的任何时候将设置按钮视为未选中的角度来解决此问题。有几种方法可以做到这一点。

一个选项是,如果所有其他按钮和触摸区域都禁用该按钮,则可以添加操作以在触摸任何其他按钮时取消选择该按钮。

UISegmentedController旨在仅允许一次选择一个项目,因此如果您的设计允许,这也可以是一个选项。

您可以查看屏幕上的所有触摸事件,如果触摸按钮外部的状态UIGestureRecognizerStateBegan,则可以将按钮设置为未选中。 这看起来像这样:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    CGPoint point = [aTouch locationInView:self.myButton.superView];
    if (!CGRectContainsPoint(self.myButton, point)) {
        // deselect button
    }
}

肯定有更多方法可以尝试解决问题,但我发现这三种方法通常涵盖了大多数情况。