问题
I want to scroll a scrollview based on the UIAccelerometer values. What is the best way to accomplish this?
Initially I set the content offset to a value based on the accelerometer values I get. Then on each scrollViewDidEndAnimating
, I again set the content offset based on the acceleromter values. I understand that setContentOffset
actually scrolls the scrollview with a uniform velocity without any acceleration. However this mechanism seems to be very jerky in scrolling the scrollview.
Any ideas will be appreciated.
Thanks and Regards
回答1:
I did this once. sv
is the scroll view.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if (sv.tracking || sv.decelerating) return;
CGPoint pt = sv.contentOffset;
pt.x += acceleration.x * 2;
pt.y -= acceleration.y * 2;
if (pt.x < 0) pt.x = 0;
if (pt.x > sv.contentSize.width - sv.frame.size.width) pt.x = sv.contentSize.width - sv.frame.size.width;
if (pt.y < 0) pt.y = 0;
if (pt.y > sv.contentSize.height - sv.frame.size.height) pt.y = sv.contentSize.height - sv.frame.size.height;
sv.contentOffset = pt;
}
来源:https://stackoverflow.com/questions/3474061/uiaccelerometer-and-uiscrollview