Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 294957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:24:52+00:00 2026-05-12T06:24:52+00:00

I have created a basic unlock slider like you use to unlock the iPhone.

  • 0

I have created a basic unlock slider like you use to unlock the iPhone.

In my touchesEnded function, I use an animation to move the slider back to home.

The first time the user moves the slider (and doesn’t actually hit the end to unlock), the animation works fine and the slider moves back to home.

However, the next time the user tries to move the slider (and touchesMoved gets called), the slider gets pushed offscreen (to the left) by the amount they had slid the slider before.

So, what’s happening?

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  self.dragging = NO;
  self.startX = 0; 
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.5];
  self.unlock.frame = CGRectMake(0, 0, 75, 35);
  [UIView commitAnimations];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if (!self.dragging ) { return; }
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint point = [touch locationInView:self];
  int offset = point.x - self.startX;
  NSLog(@"%i", offset);
  if (offset > (LENGTH-SIZE)) {
    offset = (LENGTH-SIZE);
  }
  CGAffineTransform myTransform = CGAffineTransformMakeTranslation(offset, 0.0);
  [self.unlock setTransform:myTransform];
  if (point.x >= (LENGTH-(SIZE-self.startX))) {
    NSLog(@"Slider reached end");
    [self.sliderView removeFromSuperview];
  }
}

EDIT

Here’s the working function, cleaned up a bit, though it could be better (thanks to a couple answered questions on SO from one hacker). I’m going to blog the complete module since there doesn’t seem to be a standard widget:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

  if (!self.dragging ) { return; }

  UITouch *touch = [[event allTouches] anyObject];
  CGPoint point = [touch locationInView:self];  

  if (point.x >= (LENGTH-(SIZE-self.startX))) {
    NSLog(@"Slider reached end");
    [self.sliderView removeFromSuperview];
  }

  // offset based on where the user clicked the slider
  int offset = point.x - self.startX;
  if (offset > (LENGTH-SIZE)) {
    offset = (LENGTH-SIZE);
  }

  //move the slider
  CGRect oldFrame = self.unlock.frame;
  CGRect newFrame = CGRectMake(self.frame.origin.x+offset, self.frame.origin.y-5, oldFrame.size.width, oldFrame.size.height);
  self.unlock.frame = newFrame; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:24:52+00:00Added an answer on May 12, 2026 at 6:24 am

    IN your previous question I didn’t realize you were moving the slider by changing its transform. Mixing up motion by changing its frame and transform is going to be moderately complicated.

    Maybe you could try doing something more like this:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      if (!self.dragging ) { return; }
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint point = [touch locationInView:self];
      int offset = point.x - self.startX;
      NSLog(@"%i", offset);
      if (offset > (LENGTH-SIZE)) {
        offset = (LENGTH-SIZE);
      }
    
      CGRect oldFrame = self.frame;
      CGRect newFrame = CGRectMake(oldFrame.origin.x+offset, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);
      self.frame = newFrame;
    
      if (point.x >= (LENGTH-(SIZE-self.startX))) {
        NSLog(@"Slider reached end");
        [self.sliderView removeFromSuperview];
      }
    }
    

    That moves the slider by modifying its frame, not its translation, which should cause it to interact better with the snap back animation. Alternatively, rather than resetting the frame in the implicit animator you could reset the translation there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a very basic service operation that needs to write content to
Basically I have created two MATLAB functions which involve some basic signal processing and
I am new to zend framework and having very basic doubt.I have created one
i have created below function in a file info.php to debug variable & data
I have a website where people can place bids on products. So the first
I have created a website with asp.net and uses oracle connectivity with pl/sql developer.i
I have created a VB.NET 2008 Class Library that has, for the most part,
I have created a custom content type and using views2 for creating various page-views.
I'm trying to understand some of the basics of using POSIX pthreads. The kind
OK, I'm still quite new to Xcode/Cocoa etc, so please forgive me if this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.