【问题标题】:Move views accordingly uivew collapse/expand animation相应地移动视图 uivew 折叠/展开动画
【发布时间】:2013-08-23 07:47:40
【问题描述】:

我有一个具有展开/折叠动画的自定义视图横幅,类似于此处iphone: expand and collapse a UIView programmatically 中描述的内容。

当我将该视图集成到任何现有的视图控制器时,当它扩展以向下推现有视图时,我想要什么。现在只是重叠了。

如何在 iOS 中实现这一点?在 Android 中,我使用 Visibility.Gone 完成了它,但在这里我找不到类似的东西。横幅可以在任何视图层次结构中,直接是主视图的子视图,但是当它展开时,我希望所有视图都向下移动,以使其在展开时向下推所有内容。

这里是使用 Visibility.Gone Android: Expand/collapse animation 的 Android 方法的链接

【问题讨论】:

    标签: ios objective-c uikit


    【解决方案1】:

    您可以通过操作横幅视图的框架属性及其在横幅视图层次结构中的兄弟姐妹来获得结果。所有这些都可以包含在横幅视图对象中。 frame 属性是可动画的。

    将 BannerView 作为 UIView 的子类。添加一些方法到它的公共@interface:

        - (void) collapse;
        - (void) expand;
        - (void) toggle;
    

    您将需要横幅的展开和折叠框架的几个属性:

    @property (nonatomic, assign) CGRect expandedFrame;
    @property (nonatomic, assign) CGRect collapsedFrame;
    

    这些可以放在(公共)@interface 或(私有)类别扩展中。在 BannerView 的初始化过程中设置它们:

        //initialising in code
        - (id)initWithFrame:(CGRect)frame{
            if (self = [super initWithFrame:frame]) {
                [self initialiseFrames:frame];
            }
            return self;
        }
    
        //initialising from XIB/Storyboard
        - (void)awakeFromNib {
            [self initialiseFrames:self.frame];
        }
    
        - (void)initialiseFrames:(CGRect)frame {
            self.expandedFrame = frame;
            frame.size.height = 0;
            self.collapsedFrame = frame;
        }
    

    当你展开或折叠你的bannerView时,它可以使用迭代表单迭代它的兄弟视图

    for (UIView* view in self.superview.subviews) {}
    

    通过设置它们各自的frame 属性相应地向上或向下移动它们。要升高和降低框架,请增加或减少横幅视图的高度...

    - (CGRect)lowerFrame:(CGRect)frame {
        frame.origin.y += CGRectGetHeight(self.expandedFrame);
        return frame;
    }
    
    - (CGRect)raiseFrame:(CGRect)frame {
        frame.origin.y -= CGRectGetHeight(self.expandedFrame);
        return frame;
    }
    

    将这些部分放在一起,您可以制作折叠和展开动画方法,通过设置它们的框架将兄弟视图移动到正确的位置,然后通过设置 it's 框架折叠/展开横幅视图:

    - (void) collapse {
        if (CGRectEqualToRect(self.frame, self.collapsedFrame)) return;
        [UIView animateWithDuration:0.5 animations:^{
            for (UIView* view in self.superview.subviews) {
                if (CGRectGetMinY(view.frame) > CGRectGetMaxY(self.frame))
                    view.frame = [self raiseFrame:view.frame];
            }
            self.frame = self.collapsedFrame;
        }];
    
    }
    
    - (void) expand {
        if (CGRectEqualToRect(self.frame, self.expandedFrame)) return;
        [UIView animateWithDuration:0.5 animations:^{
            for (UIView* view in self.superview.subviews) {
                if (CGRectGetMinY(view.frame) > CGRectGetMaxY(self.frame))
                    view.frame = [self lowerFrame:view.frame];
            }
            self.frame = self.expandedFrame;
        }];
    
    }
    

    ...以及在两种状态之间移动的切换方法

    - (void) toggle {
        if (CGRectEqualToRect(self.frame, self.collapsedFrame))
            [self expand];
        else [self collapseBanner];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多