[英]Transfer touches from uiview to uiscrollview
我在UIView(红色)中有一个UIScrollview(带有图像)。
UIView-{0,0,320,236} UIScrollview-{0,8,300,220}
在我的uiview中,我很感动。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
我需要将触摸从UIView转移到UIScrollview。 (例如,如果用户从uiview的右侧向左滑动,则uiscrollview应与用户触摸同步向左滚动)。 我可以知道怎么做吗?
这个想法是在UIView中获得对scrollView的引用,计算x接触增量,并相应地调整scrollView的contentOffset属性。
因此,在您的UIView类中:
@implementation MyView{
CGPoint _startPosition;
CGPoint _previousPosition;
}
在您的计算机中初始化以上变量
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
_startPosition = [touch locationInView:self];
_previousPosition = _startPosition;
}
然后触摸一下
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.scrollView];
if(!CGPointEqualToPoint(_previousPosition, _startPosition)){
CGFloat deltaX = _previousPosition.x-currentPosition.x;
[UIView animateWithDuration:0.1 animations:^{
//
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x +
deltaX, self.scrollView.contentOffset.y);
}];
}
_previousPosition = currentPosition;
}
在动画块中调整scrollView.contentOffset以使滚动平滑。
链接到正在运行的项目: https : //github.com/sliaquat/stack_overlfow_answer_28036976
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.