【问题标题】:Fading in a UIView shadow while resizing调整大小时淡入 UIView 阴影
【发布时间】:2012-10-28 03:56:55
【问题描述】:

我正在尝试在淡入阴影的同时缩放我的UIView,使用以下方法:

    myController.view.layer.shadowOffset = CGSizeMake(0, 3);
    myController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    myController.view.layer.shadowOpacity = 0.0;
    myController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:myController.view.bounds].CGPath;

    [UIView animateWithDuration:0.3
                     animations:^{
                         //shrink the view
                         myController.view.transform = CGAffineTransformMakeScale(0.8, 0.8);

                         //fade in the shadow
                         myController.view.layer.shadowOpacity = 0.8;
                     }
                     completion:^(BOOL finished){
                          ...etc

视图正确调整大小,但阴影立即出现而不是淡入。

我做错了吗?我认为shadowOpacity 是可动画的?

【问题讨论】:

    标签: objective-c ios cocoa-touch quartz-graphics


    【解决方案1】:

    您必须使用核心动画来为视图层属性设置动画:

    #import <QuartzCore/CAAnimation.h>
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.toValue = [NSNumber numberWithFloat:0.8];
    animation.duration = 1.0;
    [myView.layer addAnimation:animation forKey:@"shadowOpacity"];
    myView.layer.shadowOpacity = 0.0;
    

    【讨论】:

    • 您发送给 addAnimation: 的“anim”参数是什么?另外,最后一行是触发动画的部分,还是只是在动画后移除阴影?
    • @Ryan 抱歉,错字,已修复。
    猜你喜欢
    • 2018-10-09
    • 2011-07-11
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2010-12-18
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多