Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

how to move a view from where you touch it on the view?

Esko2300Esko2300 Posts: 501Registered Users
edited June 2012 in iPhone SDK Development
Ok what i have working are views the user can move about the screen. In my touches moved code i am moving the view about it center. Ultimately what i would want to do is be able to move the view at whichever point the user touches the view on. Any more details just ask but i think thats good enough
Post edited by Esko2300 on

Replies

  • BrianSlickBrianSlick Posts: 9,398Tutorial Authors, Registered Users
    edited May 2012
    When it is first touched, make a note of how far away the touch is from the view center. When adjusting the center, include that adjustment in the calculation.

    One of the WWDC videos from last year, I believe it focused on scroll views, shows an example.
  • Duncan CDuncan C Posts: 8,135Tutorial Authors, Registered Users
    edited May 2012
    BrianSlick;434908 said:
    When it is first touched, make a note of how far away the touch is from the view center. When adjusting the center, include that adjustment in the calculation.

    One of the WWDC videos from last year, I believe it focused on scroll views, shows an example.
    Or note the original center, and the original touch point for the touchesBegan. When the user drags their finger, move the view's center by the same offset.

    Gesture recognizers make this easier. Do a search in "Touches" in the Xcode help system, and look under Sample code for an entry with that name. It is a sample project that shows exactly how to do this. Note that there are actually 2 versions of the project, one that uses touchesBegan/touchesMoved/touchesEnded, and one that uses a pan gesture recognizer. I suggest looking at the gesture recognizer version. The code is much cleaner and easier to maintain.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Esko2300Esko2300 Posts: 501Registered Users
    edited May 2012
    Well i already have code i am using in TouchesMoved ect. so imma try to stick with that. I checked the Touches app and the code using TouchesMOved only moves the view about the center and not the initial touch point. Ill give your ideas with what to do with the initial point and center of the object and if i can't figure it out ill go with the UIPanGesture method instead. Thanks to both of you
  • Esko2300Esko2300 Posts: 501Registered Users
    edited June 2012
    well i got it working heres the code to do it

    in your touchesBegan method just take the offset of the view in relation to the point of touch and the center

    if([t view] == moveableSlide || [t view] == insertionSlide){
    Slide *s = [t view] == moveableSlide ? moveableSlide:insertionSlide;
    offset = CGPointMake([t locationInView:self].x - s.center.x, [t locationInView:self].y - s.center.y);


    Then in touches moved just set the center of the view to the touchLocation minus the center


    s.center = CGPointMake([t locationInView:self].x - offset.x, [t locationInView:self].y - offset.y);
  • Duncan CDuncan C Posts: 8,135Tutorial Authors, Registered Users
    edited June 2012
    Esko2300;437174 said:
    well i got it working heres the code to do it

    in your touchesBegan method just take the offset of the view in relation to the point of touch and the center

    if([t view] == moveableSlide || [t view] == insertionSlide){
    Slide *s = [t view] == moveableSlide ? moveableSlide:insertionSlide;
    offset = CGPointMake([t locationInView:self].x - s.center.x, [t locationInView:self].y - s.center.y);


    Then in touches moved just set the center of the view to the touchLocation minus the center


    s.center = CGPointMake([t locationInView:self].x - offset.x, [t locationInView:self].y - offset.y);

    Code in a release app would need range checking to provent the user from dragging the view completely off the screen.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Esko2300Esko2300 Posts: 501Registered Users
    edited June 2012
    Duncan C;437177 said:
    Code in a release app would need range checking to provent the user from dragging the view completely off the screen.
    Good point, my code does that already since if the user lifts their finger off the view the view automatically disappears but its good for whomever will read this to know they must remove the view somehow
Sign In or Register to comment.