我有三个视图控制器是UIScrollView的一部分 . 我希望能够在三者之间滑动,尽管其中一个视图控制器有一个UIPanGestureRecognizer . 我使用这个平移手势识别器允许用户上下拖动手指来增加和减少矩形UIView的高度 . 因此,这个UIPanGestureRecognizer只需要知道向上/向下平移,滚动视图可以使用水平平移 .
这方面的一个例子就像主屏幕;你可以向左或向右滑动,但也可以向下滑动以获得聚光灯 . 我想要这种机制 .
这是我的泛码:
- (void)pan:(UIPanGestureRecognizer *)aPan; // When pan guesture is recognised
{
CGPoint location = [aPan locationInView:self.view]; // Location of finger on screen
CGRect secondRect = CGRectMake(210.0, 45.0, 70.0, 325.0); // Rectangles of maximimum bar area
CGRect minuteRect = CGRectMake(125.0, 45.0, 70.0, 325.0);
CGRect hourRect = CGRectMake(41.0, 45.0, 70.0, 325.0);
if (CGRectContainsPoint(secondRect, location)) { // If finger is inside the 'second' rectangle
CGPoint currentPoint = [aPan locationInView:self.view];
currentPoint.y -= 80; // Make sure animation doesn't go outside the bars' rectangle
if (currentPoint.y < 0) {
currentPoint.y = 0;
}
else if (currentPoint.y > 239) {
currentPoint.y = 239;
}
currentPoint.y = 239.0 - currentPoint.y;
CGFloat pointy = currentPoint.y - fmod(currentPoint.y, 4.0);
[UIView animateWithDuration:0.01f // Animate the bars to rise as the finger moves up and down
animations:^{
CGRect oldFrame = secondBar.frame;
secondBar.frame = CGRectMake(oldFrame.origin.x, (oldFrame.origin.y - (pointy - secondBar.frame.size.height)), oldFrame.size.width, (pointy));
}];
CGFloat result = secondBar.frame.size.height - fmod(secondBar.frame.size.height, 4.0);
secondInt = (result / 4.0); // Update labels with new time
self->secondLabel.text = [NSString stringWithFormat:@"%02d", secondInt];
}
代码基本上针对三个独立的矩形UIView重复 .
如果有人能告诉我如何将主屏风格的平移/滑动到我的应用程序中,那将是非常棒的!
1 回答
Alright, here is the short answer:
你必须使用
UIGestureRecognizer
的方法-requireGestureRecognizerToFail:
.And here is the long answer:
如果
TimerViewController
的平移手势识别器失败,则必须使滚动视图的平移手势识别器成功 only . 但是,如果 initial 运动是垂直的,那么该手势(TimerViewController
的手势)应该成功 . 如果它是水平的,它应该失败 . 为了实现这一点,我们必须继承UIPanGestureRecognizer
并对其进行修改以满足这些需求 . 这是你要做的:忽略您之前的答案所做的所有更改
将
VerticalPanGestureRecognizer
添加到您的项目中 .修改
TimerViewController
,如图所示 .修改
ScrollViewController
,如图所示 .VerticalPanGestureRecognizer.h
VerticalPanGestureRecognizer.m
TimerViewController.h
TimerViewController.m
ScrollViewController.m
这种新方法非常有效 . 我测试了它 . 如果您有更多问题,请与我们联系 .
干杯!
UPDATE
要回答您在评论中发布的问题,请执行以下操作:
再次,我测试了它,它正在工作 . 您只需为条形图添加手势识别器