IOS UIButton Long Press events

Source: Internet
Author: User

UIButton *abtn=[uibutton Buttonwithtype:uibuttontypecustom];
[Abtn Setframe:cgrectmake (40, 100, 60, 60)];
[Abtn setbackgroundimage:[uiimage imagenamed:@ "111.png"] forstate:uicontrolstatenormal];
button click event
[Abtn addtarget:self Action: @selector (Btnshort:) forcontrolevents:uicontroleventtouchupinside];
Button Long press event
Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (Btnlong:)];
Longpress.minimumpressduration = 0.8; Define the time to press
[Abtn addgesturerecognizer:longpress];

-(void) Btnlong: (Uilongpressgesturerecognizer *) gesturerecognizer{
if ([gesturerecognizer state] = = Uigesturerecognizerstatebegan) {
NSLog (@ "Long press event");
Uialertview *alert=[[uialertview alloc] initwithtitle:@ "message" message:@ "OK to delete this mode? "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" delete ", nil";
[Alert show];
}
}

Another kind

-(ID) initWithFrame: (CGRect) Frame {

UITableView *tmptblview = [[UITableView alloc] initWithFrame:self.frame];

.......

Instantiate long press gesture monitoring

Uilongpressgesturerecognizer *longpress =

[[Uilongpressgesturerecognizer alloc] Initwithtarget:self

Action: @selector (handletableviewcelllongpressed:)];

Agent

Longpress.delegate = self;

Longpress.minimumpressduration = 1.0;

Add a long-tap gesture to a view that needs to implement a long-press operation

[Tmptblview addgesturerecognizer:longpress];

[Longpress release];

[Tmptblview release];

}

How to implement long-press events

-(void) handletableviewcelllongpressed: (Uilongpressgesturerecognizer *) Gesturerecognizer {

if (gesturerecognizer.state = = Uigesturerecognizerstatebegan) {

NSLog (@ "Uigesturerecognizerstatebegan");

}

if (gesturerecognizer.state = = uigesturerecognizerstatechanged) {

NSLog (@ "uigesturerecognizerstatechanged");

}

if (gesturerecognizer.state = = uigesturerecognizerstateended) {

NSLog (@ "uigesturerecognizerstateended");

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.