Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make scrollView 'bounce' on tap

For example, on the iOS 7/8 lock screen, if you tap rather than drag it to the right, it will have a slight bounce effect. How can I detect the tap gesture (and not have it be confused with the dragging) and recreate a similarly subtle 'bounce' effect? Can anybody share a code example (swift/obj-c) ?

I think this is a great way to show the user that something should be dragged rather than tapped and it doesn't require reading any small indicators.

like image 217
user3721428 Avatar asked Sep 29 '22 03:09

user3721428


1 Answers

Add tap gesture to your View like this:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTap];

Can be done using UIView's animateWithDuration method in handleTap: method

- (void)handleTap:(UITapGestureRecognizer*)gesture
{
  __block CGRect frame = scrollView.frame;

  [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         frame.origin.x += 20.0f;
                         scrollView.frame = frame;
                     }
                     completion:^(BOOL finished){
                         scrollView.frame = frame;
                     }];
}
like image 92
Paresh Navadiya Avatar answered Oct 23 '22 01:10

Paresh Navadiya