UIViews和Pan手势的手动转换进度

时间:2013-11-22 12:48:48

标签: ios objective-c uikit catransition

我正在尝试实现某种可移动视图,根据父视图框架大小和位置改变它自己的框架。 我发现的唯一决定是在superview的框架中添加一个观察者

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [self.superview removeObserver:self forKeyPath:@"frame"];
    [newSuperview addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}

然后,如果superview的帧更改

,我将通过委托更改视图的帧
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if([keyPath isEqualToString:@"frame"])
    {
        if([self.superview respondsToSelector:@selector(progressForMovedView:)])
        {
            CGFloat progress = [(id <MovableViewProgressProtocol>)self.superview progressForMovedView:self];

            self.frame = [self proposedFrameForFrame:_startFrame targetFrame:_endFrame andProgress:progress];
        }
    }
}

- (CGRect)proposedFrameForFrame:(CGRect)baseFrame targetFrame:(CGRect)targetFrame andProgress:(CGFloat)progress
{
    CGRect frame = baseFrame;

    frame.origin.x      += (targetFrame.origin.x - frame.origin.x) * progress;
    frame.origin.y      += (targetFrame.origin.y - frame.origin.y) * progress;
    frame.size.width    += (targetFrame.size.width - frame.size.width) * progress;
    frame.size.height   += (targetFrame.size.height - frame.size.height) * progress;

    return frame;
}

最后它看起来像gif:

enter image description here

还有其他方法可以达到同样的效果吗?

0 个答案:

没有答案