简体   繁体   English

touchesMoved事件未获得正确的CGPoint

[英]touchesMoved event not getting the right CGPoint

I was trying to implement a scrollView by setting the bounds while paning the view 我试图通过在平移视图时设置界限来实现scrollView
I was doing a test in touchesMoved : 我正在touchesMoved进行测试:

UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
if(pt.x - _lastPoint.x >= 0){
    NSLog(@"yep");
}
else{
    NSLog(@"nope");
    NSLog(@"%f",pt.x);
}
_lastPoint.x = pt.x;
[self setBounds:CGRectMake(self.bounds.origin.x - 1, 0,  self.frame.size.width, self.frame.size.height)];

And while I was paning in one direction(from left to right all the way ),I can always get a few bad points,here is one of the logs: 虽然我是在一个方向paning(左到右一路 ),我总能得到一些不尽如人意的地方,这里是某一条记录:

2014-04-26 14:59:41.621 LScrollView[9884:60b] yep
2014-04-26 14:59:41.638 LScrollView[9884:60b] yep
2014-04-26 14:59:41.656 LScrollView[9884:60b] yep
2014-04-26 14:59:41.673 LScrollView[9884:60b] yep
2014-04-26 14:59:41.696 LScrollView[9884:60b] yep
2014-04-26 14:59:41.713 LScrollView[9884:60b] yep
2014-04-26 14:59:41.729 LScrollView[9884:60b] yep
2014-04-26 14:59:41.762 LScrollView[9884:60b] yep
2014-04-26 14:59:41.796 LScrollView[9884:60b] nope
2014-04-26 14:59:41.797 LScrollView[9884:60b] 143.000000
2014-04-26 14:59:41.813 LScrollView[9884:60b] yep
2014-04-26 14:59:41.829 LScrollView[9884:60b] yep
2014-04-26 14:59:41.846 LScrollView[9884:60b] yep

And I also wrote a test that not changing the bounds of the view in touchesMoved and everything works fine,did I missing something? 而且我还编写了一个测试,该测试touchesMoved更改视图的边界 ,并且一切正常,是否丢失了某些内容?

There is no need to change the bounds of the view while you are dragging the finger on it. 在手指上拖动视图时,无需更改视图的边界。 You are always making your origin 1 pixel back on left side. 您总是将原点向左移1像素。 I mean if it is at x = 0 on next event it will be shifted to x = -1. 我的意思是,如果下一次事件在x = 0处,它将移至x = -1。

Till the time next event is generated and you have not dragged your finger for 1 pixel in right direction, app will return you the point which will not able to fulfil your if condition. 直到生成下一个事件的时间,并且您没有向正确的方向拖动手指1个像素,应用程序才会向您返回无法满足if条件的点。 So, remove setBounds on each dragging event or drag the finger very fast. 因此,在每个拖动事件上删除setBounds或非常快速地拖动手指。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM