开发者

UIAccelerometer and UIScrollView

开发者 https://www.devze.com 2023-01-10 01:31 出处:网络
I want to scroll a scrollview based on the UIAccelerometer values. What is the best way to accomplish this?

I want to scroll a scrollview based on the UIAccelerometer values. What is the best way to accomplish this?

My main problem is that using the accelerometer values, I cannot set a content offset. Because the scrollview itse开发者_开发技巧lf scrolls using a particular velocity and there are jerks happening when I try to scroll using accelerometer values.


Try this:

- (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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号