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.