IOS development-drag images

Source: Internet
Author: User
@ Interface placement () {@ private cgpoint startpoint;} @ end @ implementation aplmovemeview @ synthesize placardview; // @ synthesize nextdisplaystringindex;-(void) setupplacecardview :( aplplacardview *) _ placecardviews {self. placardview = _ placecardviews;}-(void) touchesbegan :( nsset *) touches withevent :( uievent *) event {// we only support single touches, so anyobject retrieves just that touch from touches. Uitouch * Touch = [touches anyobject]; // only move the placard view if the touch was in the placard view. If ([Touch view]! = Self. placardview) {// in case of a double tap outside the placard view, update the placard's display string. if ([Touch tapcount] = 2) {[self setupnextdisplaystring];} return;} cgpoint point = [[touches anyobject] locationinview: Self]; startpoint = point; // animate the first touch. cgpoint touchpoint = [Touch locationinview: Self]; [self animatefirsttouchatpoint: touchpoint];}-(void) touchesmoved :( nsset *) touches withevent :( ui* event *) event {uitouch * Touch = [touches anyobject]; // If the touch was in the placardview, move the placardview to its location. if ([Touch view] = self. placardview) {// cgpoint locationss = [Touch locationinview: Self]; // self. placardview. center = locationss; // 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 (startpoint. x, 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); // y coordinate 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. placardview. center = newcenter; return ;}- (void) touchesended :( nsset *) touches withevent :( uievent *) event {uitouch * Touch = [touches anyobject]; // If the touch was in the placardview, bounce it back to the center. if ([Touch view] = self. placardview) {/* Disable User interaction so subsequent touches don't interfere with animation until the placard has returned to the center. interaction is reenabled in animationdidstop: Finished :. */self. userinteractionenabled = no; [self animateplacardviewtocenter]; return ;}}

You can use the built-in IOS instance moveme to drag images and drag images within the screen. Below are some of the changed code:

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.