来自笔尖的圆形UIView,带有动画和非动画更改

时间:2017-07-28 18:35:13

标签: ios objective-c iphone uiview calayer

我想创建一个自定义视图,当我进行边界,框架或转换更改时,就像UIView 一样:如果我通过赋值更改这些内容,只需更改一个帧,如果我在UIView动画块中更改它们,它们会从当前状态设置为我设置的状态。我想在IB中定义视图及其子视图,并在那里设置约束。

以下代码使用约束从nib中运行,并使用赋值(无动画)成功设置bounds / frame / transform。但是我无法让动画正常工作。

特别是,遮罩层的动画似乎出错了。目前(即使我认为layoutSubviews应该通过匹配视图动画来处理它),掩码会跳过视图宽度/高度的一半,然后在动画进行时动画回到居中位置。

@implementation BubbleView

+ (instancetype)bubbleViewFromNib {
    BubbleView *view = [[NSBundle mainBundle] loadNibNamed:@"BubbleView" owner:nil options:nil][0];
    return view;
}

// this works
- (void)setMaskImageName:(NSString *)name {
    self.maskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // get current animation for bounds
    CAAnimation *anim = [self.layer animationForKey:@"bounds"];

    [CATransaction begin];
    if(anim) {
        // animating, apply same duration and timing function.
        [CATransaction setAnimationDuration:anim.duration];
        [CATransaction setAnimationTimingFunction:anim.timingFunction];

        self.maskView.frame = self.bounds;
    }
    else {
        // not animating, we should disable implicit animations.
        [CATransaction disableActions];
    }

    self.maskView.frame = self.bounds;
    [CATransaction commit];
}
@end

// calling view controller

// the code in this block doesn't work
[UIView animateWithDuration:1 animations:^{
    //b.transform = CGAffineTransformScale(b.transform, 1.4, 1.4);
    b.frame = CGRectInset(b.frame, -30,-30);
}];

// the same code works fine without animation
//b.frame = CGRectInset(b.frame, -30,-30);

我从https://stackoverflow.com/a/25458725/1272965获得了layoutSubviews的想法,并在我之前提出的问题https://stackoverflow.com/a/45356867/1272965的帮助下得到了一些帮助(这是关于动画时的变化这些道具简单分配)。

你能帮助我让我的UIView掩码像iOS提供的任何视图一样工作,保持约束并更改动画或不动画的帧吗?谢谢!

0 个答案:

没有答案