Prerequisites
IOS processes the touch action on the screen, mainly involving the following methods:
Touchesbegan: withevent: // the initial call of the touch screen
Touchesmoved: withevent: // called during movement
Touchesended: withevent: // called when the action ends
Touchescancelled: withevent:
The method name clearly shows when the method is called, and the last one is special.Touchescancelled: withevent:InCocoa touchMust be called in response to a system interruption of a persistent touch event.
We only need to rewrite these methods to do what we want to do.
How to drag a view? 1. Set the userinteractionenabled attribute to yes to allow user interaction. 2. Record the start point at the start of the touch action. 3. During the movement process, the difference between the current Coordinate Position and the starting point is calculated, that is, the offset, and the center point of the view is moved to the offset. 4. restrict the X coordinates and Y coordinates respectively to ensure that the user cannot bring the view out of the screen. Note: The reason that the X coordinate and Y coordinate are respectively restricted is that even if you cannot drag to the right, you still need to ensure that you can drag down. Implementation Code Taking subclass uiimageview as an Example
# Import <uikit/uikit. h> @ interface gragview: uiimageview {cgpoint startpoint;} @ end
# Import "gragview. H "@ implementation gragview-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {// initialization code // allow user interaction with self. userinteractionenabled = yes;} return self;}-(ID) initwithimage :( uiimage *) image {self = [Super initwithimage: Image]; If (Self) {// allow user interaction self. userinteractionenabled = yes;} return self;}-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event {// Save the position where the touch starts cgpoint = [[touches anyobject] locationinview: Self]; startpoint = point; // This view is placed at the beginning of [[self superview] bringsubviewtofront: self];}-(void) touchesmoved :( nsset *) touches withevent :( uievent *) event {// calculates the displacement = Current Position-starting position cgpoint point = [[touches anyobject] locationinview: Self]; float dx = point. x-startpoint. x; float DY = point. y-startpoint. y; // calculate the moving View Center cgpoint newcenter = cgpointmake (self. center. X + dx, self. center. Y + dy);/* restrict users from holding Views out of the screen */float halfx = cgrectgetmidx (self. bounds); // returns the left boundary of the X coordinate newcenter. X = max (halfx, newcenter. x); // The right boundary of the X coordinate newcenter. X = min (self. superview. bounds. size. width-halfx, newcenter. x); // The Y coordinate is the same as float Halfy = cgrectgetmidy (self. bounds); newcenter. y = max (Halfy, newcenter. y); newcenter. y = min (self. superview. bounds. size. height-Halfy, newcenter. y); // move view self. center = newcenter;}/* // only override drawrect: If you perform custom drawing. // an empty implementation adversely affects performance during animation. -(void) drawrect :( cgrect) rect {// drawing code} */@ end