长按手势时在 UIButton 中显示图像
我有以下问题。我有一个 UIScrollView ,上面有几个按钮,这些按钮上的图标设置为图像。我的每个按钮都附有一个长按识别器。如何在长按手势时在发件人按钮上显示较小的删除图标?我的目标是创建当用户想要删除特定应用程序时 iOS 呈现的行为。 这是按钮的代码(带有图像):
//set the button with the image of conference here.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3, 3, w-5, h-5);
CALayer * l = [button layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[button setImage:thumb forState:UIControlStateNormal];
button.property = confInfo;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
bView.tag = i;
//Add gesture recognizer to be used for deletion of conference.
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer.minimumPressDuration = 1.0;
[button addGestureRecognizer:pahGestureRecognizer];
此代码位于循环中(请参阅代码中的 i)。我的长按操作是这样的:
- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateEnded:
NSLog(@"Tapped!!!");
break;
default:
break;
}
}
如何将我单击的按钮传递给此操作以在按钮的右上角显示较小的 X 图像?
I have the following problem. I have a UIScrollView
on which I have a couple of buttons with icons set as images on these buttons. I have a long press recognizer attached to each button. How can I show a smaller delete icon on the sender button on long press gesture? My goal is to create the behaviour that is presented by iOS when the user wants to delete a specific application.
This is the code for a buttons (with images):
//set the button with the image of conference here.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3, 3, w-5, h-5);
CALayer * l = [button layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[button setImage:thumb forState:UIControlStateNormal];
button.property = confInfo;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
bView.tag = i;
//Add gesture recognizer to be used for deletion of conference.
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];
pahGestureRecognizer.minimumPressDuration = 1.0;
[button addGestureRecognizer:pahGestureRecognizer];
This code is in a cycle (see i in the code). My long tap action is like this:
- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateEnded:
NSLog(@"Tapped!!!");
break;
default:
break;
}
}
How I can pass a button on which I clicked to this action to show the smaller X image on the right upper corner of the button ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的手势识别器应通过 UIButton 的 view 属性附加到它。
Your gesture recognizer should be attached to the UIButton via its view property.