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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "none" is not an acceptable value for the "Accept-Encoding" header.… May 12, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer customer is probably not going to want to spend additional… May 12, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer XmlReader.Create allows you to specify XmlReaderSettings, which none of the… May 12, 2026 at 6:25 pm

Related Questions

I'm working on a simulation that uses multithreading extensively. The thing is that, until
How do I do the IDE method or eventually make the basic dock method
I have created a basic WCF service in IIS. I am aware that this
I have created a basic site using ASP.NET routing according to Mike Ormond's example

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.