简体   繁体   English

自定义UIView调整大小,同时识别平移手势

[英]Custom UIView resizing while recognizing pan gesture

I'm trying to achieve the following effect. 我试图达到以下效果。 I have a view which can be in two different states - collapsed and expanded). 我有一个可以处于两种不同状态的视图-折叠和展开)。 See screenshots for what I have in mind: 请参阅屏幕截图以了解我的想法:

倒塌展开式

Transition between these states should be triggered by panning gesture - user panned down view gets expanded, user panned up - view gets collapsed. 这些状态之间的转换应通过平移手势来触发-用户平移视图将展开,用户平移视图将折叠。

I can achieve this by implementing custom gesture recognizer in the init section of my view subclass: 我可以通过在视图子类的init部分中实现自定义手势识别器来实现此目的:

UIPanGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(panned:)];
recognizer.delegate = self;
[self addGestureRecognizer: recognizer];

And then detecting proper vertical pan gesture: 然后检测正确的垂直平移手势:

- (void)panned:(UIPanGestureRecognizer*)gestureRecognizer
{
    static CGPoint lastPoint;
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        lastPoint = [gestureRecognizer locationInView: self];
        return;
    }

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        if (p.y > lastPoint.y && self.expanded == NO) {
            [self toggle];
        }
        else if (p.y < lastPoint.y && self.expanded == YES)
        {
            [self toggle];
        }
    }
}

Then based on self.expanded property I layout subviews and change frame accordingly. 然后基于self.expanded属性,我布置子视图并相应地更改框架。 Everything works more or less ok. 一切正常或多或少。 The only issue I have is - I'd like to show transition between two states as user panning his finger - so basically view should start expanding gradually back and forth (if user pans back and forth) and then come to the expanded state when gesture is complete. 我唯一的问题是-我想在用户平移手指时显示两种状态之间的过渡-因此基本上,视图应开始逐渐来回扩展(如果用户来回摇动),然后在手势时进入扩展状态完成了。

What's the easiest way to do this? 最简单的方法是什么?

What I would do is devise an animation between the collapsed state and the expanded state. 我要做的是在折叠状态和展开状态之间设计一个动画。 (This might involve changing one view over time, or fading / moving between two different views, one in the collapsed state and the other in the expanded state.) Now drive the animation in accordance with the gesture, as I explain here: (这可能涉及随着时间的推移更改一个视图,或者在两个不同的视图之间淡入/移动,一个处于折叠状态,另一个处于展开状态。)现在按照手势驱动动画,如我在此处说明的那样:

https://stackoverflow.com/a/22677298/341994 https://stackoverflow.com/a/22677298/341994

Basically, you attach the animation to a layer, with its speed set to 0 so that nothing actually happens. 基本上,您将动画附加到图层上,其speed设置为0,因此实际上什么也没有发生。 Then you track the gesture and keep changing the timeOffset of the layer to change the "frame" of the animation to match. 然后,您跟踪手势并不断更改图层的timeOffset以更改动画的“帧”以使其匹配。

(One can't help observing, in this connection, that this is what is already done for you by a custom transition animation - ie, a transition between the views of two view controllers where you add your own interactive animation. So, if you are iOS 7 only, it might be simplest to actually use a custom transition animation. That actually is how the weekly-monthly transition in the Calendar app is achieved, which you so appositely mention in a comment - it's just a push/pop transition with a custom interaction animation.) (在这种情况下,一个人不禁观察到,自定义过渡动画已经为您完成了这一工作-即,在两个视图控制器的视图之间进行过渡,并在其中添加了自己的交互式动画。因此,如果您仅适用于iOS 7,实际上使用自定义过渡动画可能是最简单的,这实际上就是在Calendar应用中实现每周一次的每月过渡的方式,您在注释中适当地提到了-这只是一个推送/弹出式过渡,自定义互动动画。)

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

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