The use of the iOS touch event is detailed

Source: Internet
Author: User

Touch events are the most commonly used events in iOS, and here we introduce the next touch event.

Define Uiimageview in the following example. First we add touch events to the Toucheventviewcontroller and move the image using the touch move event, which is as follows:

@implementation Touchevenviewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Uiimageview * _image=[[kcimage Alloc]initwithframe:cgrectmake (50, 50, 150, 169)];
[_image setimage:[uiimage imagename:@ "Zmit"]];
[Self.view Addsubview:_image];
}

#pragma the touch events of the mark-View controller
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{
NSLog (@ "Start touch");
}

-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{
Get a Touch object (there may be multiple objects for multi-touch)
Uitouch *touch=[touches Anyobject];
Get Current position
Cgpoint Current=[touch LocationInView:self.view];
Get the previous position
Cgpoint Previous=[touch PreviousLocationInView:self.view];
The midpoint position before the move
Cgpoint Center=_image.center;
Move offset
Cgpoint Offset=cgpointmake (current.x-previous.x, CURRENT.Y-PREVIOUS.Y);

Reset New Location
_image.center=cgpointmake (Center.x+offset.x, CENTER.Y+OFFSET.Y);

NSLog (@ Touch);

}

-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{
NSLog (@ "Touch End");
}
@end
A simple drag touch event is shown here for everyone.

Related Article

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.