louisD
louisD

Reputation: 175

scrollViewWillEndDraggin:WithVelocity:targetContentOffset delegate method bug

I am implementing a UIScrollView and its delegate in a UIViewController. It scrolls in the horiztonal way.

WHat I want is to set "magnetism" when the view is dragged and is stabilizing.

to do that, I am listening to the delegate, specially the method mentionned in titled. It returns me the offset of the final destination. then I make the scollview display the view which correspond to this destionation with setContentOffset or setvisiblerect methods.

I also NSlog the entire method to catch the bugs.specially right after calling the method I have a nslog which confirmed me/or not, if the method is triggered.

problem : when I build&run, the first nslog is triggered an average of 3 times by dragging. so the entire effect looks very strange and I can't figure out whether or not it works.

I have found very little things about this on the net. So it would be a great help if you had some clues. cheers

I am kind of desperate :/

Upvotes: 0

Views: 224

Answers (1)

jacerate
jacerate

Reputation: 456

So you want the scroll view to snap to a valid selection when the user is done dragging? You should be able to implement this using the

- (void)scrollViewWillBeginDragging:(UIScrollView *)thisScrollView

and

- (void)scrollViewWillDidScroll:(UIScrollView *)thisScrollView

methods. Without seeing your code I can't tell you exactly how to go about this, but I would recommend keeping track of the thisScrollView.contentOffset.x in a variable, and doing some arithmetic to move the scrollView with a

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations]; 

call to move it to the appropriate spot.

Upvotes: 1

Related Questions