Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth move of the contentoffset UIScrollView Swift

Tags:

I would like to set the contentOffset of my scrollview programmatically when the contentOffset is between two points (please see the picture below) with Swift.

The problem is, I would like to add a smooth transition for the move, but I didn't find documentation for this. I tried to do a loop in order to decrease gradually the content offset but the result is not so good.

With this example, if the content offset is less than 150 px at the end of the scroll, it goes smoothly (duration of the animation would be 1 sec) to the point with the offset equal to 100. Up to 150 px, it goes to 200px.

If you can provide me directions (documentation or quick example) of what to do it would be great :) Thank you !

enter image description here

like image 236
Jibeee Avatar asked Nov 25 '15 14:11

Jibeee


People also ask

What are the best instance properties for UIScrollView?

UIScrollView has a lot of instance properties, but contentInset, contentOffset, and contentSize are probably the most frequently used. One can do wonders with a complete understanding of these properties—starting with a stretchable toolbar to pinch in pinch out of images and a lot of other fun stuff.

What is contentinset in UIScrollView?

contentInset is margin from UIScrollView to innerView. To give inner space to childView. This is given at the time the view is being laid out. This is set programmatically only.

What is the difference between contentoffset and contentinset?

contentOffset is where the user is standing after scrolling the area. So this would change each and every time the user scrolls up and down. At times this can be set programmatically as well as in main thread, which will scroll up to given value if the position exists. contentInset is margin from UIScrollView to innerView.


2 Answers

You can use UIView.animations

  func goToPoint() {     dispatch_async(dispatch_get_main_queue()) {       UIView.animateWithDuration(2, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {         self.scrollView.contentOffset.x = 200         }, completion: nil)     }   } 

Swift version

DispatchQueue.main.async {     UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {         self.myScrollView.contentOffset.x = CGFloat(startingPointForView)     }, completion: nil) } 

Swift 4

DispatchQueue.main.async {     UIView.animate(withDuration: 1, delay: 0, options: UIView.AnimationOptions.curveLinear, animations: {             self.scrollView.contentOffset.x = 200     }, completion: nil) } 
like image 145
fatihyildizhan Avatar answered Oct 12 '22 02:10

fatihyildizhan


Here is the Swift 3 version of fatihyildizhan's code.

DispatchQueue.main.async {     UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {         self.myScrollView.contentOffset.x = CGFloat(startingPointForView)     }, completion: nil) } 
like image 25
Darko Avatar answered Oct 12 '22 01:10

Darko