gpt4 book ai didi

ios - 如果在 UIButton 之外触摸,如何更改 UIButton 的边框?

转载 作者:行者123 更新时间:2023-11-29 12:10:56 25 4
gpt4 key购买 nike

目前我正在尝试创建一个对象的刻板“选定” Action 。也就是说,当我单击对象时,它的边框会变为不同的颜色,而当我在对象外部单击时,边框会变回其正常颜色。我可以弄清楚如何在触摸对象内部(在本例中为 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 的边框更改回正常状态,以获得真正的“选择/取消选择”感觉。

最佳答案

似乎问题不在于您不能将边框改回原样,而在于当用户触摸按钮外部时无法触发操作。

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

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

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
}
}

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

关于ios - 如果在 UIButton 之外触摸,如何更改 UIButton 的边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com