Autolayout:调整视图动画不起作用

时间:2015-01-17 17:03:34

标签: ios objective-c animation autolayout

我现在正在使用自定义UITextField,我的主要目标是提供自定义占位符动画。我想简单地调整占位符的大小并将其移动到左上角。在gif bellow中,你可以看到向左上方移动效果很好,但调整大小不是动画,我不知道如何。这两个动作都以与自动布局相同的方式进行动画处理。根据我阅读的内容,它应该有效,适用于自动布局中的任何其他动画,排除视图大小调整。

思考/评论?我到底做错了什么?

我目前的实施:

#import "LTTextField.h"
#import "PureLayout.h"
#import <QuartzCore/QuartzCore.h>

@interface LTTextField()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *betterPlaceholder;
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;

@end

@implementation LTTextField

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setBorderStyle:UITextBorderStyleNone];
        self.delegate = self;

        self.betterPlaceholder = [[UILabel alloc] initForAutoLayout];

        [self.betterPlaceholder setFont:[UIFont systemFontOfSize:17.0f]];
        [self.betterPlaceholder setAdjustsFontSizeToFitWidth:YES];
        [self.betterPlaceholder setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];

        [self addSubview:self.betterPlaceholder];

        [self.betterPlaceholder autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:self];
        [self.betterPlaceholder autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self];
        [self.betterPlaceholder autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:self.betterPlaceholder];
        self.heightConstraint = [self.betterPlaceholder autoSetDimension:ALDimensionHeight toSize:CGRectGetHeight(self.frame)];

        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];

        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    }
    return self;
}

- (void)drawPlaceholderInRect:(CGRect)rect {}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self refreshPlaceHolderText];
}

- (void)refreshPlaceHolderText{
    if (self.placeholder) {
        if (self.attributedPlaceholder) {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedPlaceholder];
            [attributedString setAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} range:NSMakeRange(0, attributedString.string.length)];
            [self.betterPlaceholder setAttributedText:attributedString];
        } else {
            [self.betterPlaceholder setText:self.placeholder];
        }
        NSLog(@"Placeholder text %@",self.placeholder);
    }
}

- (void)animatePlaceholderToState:(LTPlaceholderState)state animated:(BOOL)animated {

    if (LTPlaceholderStateStart == state) {
        self.heightConstraint.constant = CGRectGetHeight(self.frame);
    } else if (LTPlaceholderStateEnd == state) {
        self.heightConstraint.constant = 20;
    }

    [UIView animateKeyframesWithDuration:1.0f
                                   delay:0.0f
                                 options:UIViewKeyframeAnimationOptionBeginFromCurrentState
                              animations:^{
                                  [self setNeedsLayout];
                                  [self layoutIfNeeded];
                              } completion:nil];
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidChange:(UITextField *)theTextField{
    LTPlaceholderState placeholderState;
    if( theTextField.text.length > 0 ) {
          placeholderState = LTPlaceholderStateEnd;
    } else {
          placeholderState = LTPlaceholderStateStart;
    }
    [self animatePlaceholderToState:placeholderState animated:self.editing];
}

@end

Current implementation example

1 个答案:

答案 0 :(得分:0)

看起来你的动画工作正常,只是字体大小会捕捉到较小的字体。您使用autoMatchDimension强制标签的宽度和高度相同,并且您已请求setAdjustsFontSizeToFitWidth:YES。因此,在计算新布局时,字体大小会减小(或增加,具体取决于它是否为动画制作或输出)。字体大小不是可动画的属性,因此更改会立即发生。

您可能不会更改视图框架高度,而是动画视图的比例和框架原点的更改。这将确保它变小,但实际上并不需要动画文本属性更改。

相关问题