Ios 自动布局:调整视图动画大小不起作用

Ios 自动布局:调整视图动画大小不起作用,ios,objective-c,animation,autolayout,Ios,Objective C,Animation,Autolayout,我现在正在处理自定义UITextField,我的主要目标是提供自定义占位符动画。我只想调整占位符的大小并将其移动到左上角。在下面的gif文件中,你们可以看到移到左上角效果很好,但调整大小并没有动画效果,我也不知道该怎么做。这两个动作的动画方式与自动布局相同。根据我读到的内容,它应该可以工作,并且适用于自动布局中的任何其他动画,排除视图调整大小 想法/评论?我到底做错了什么 我目前的执行情况: #import "LTTextField.h" #import "PureLayout.h" #impo

我现在正在处理自定义UITextField,我的主要目标是提供自定义占位符动画。我只想调整占位符的大小并将其移动到左上角。在下面的gif文件中,你们可以看到移到左上角效果很好,但调整大小并没有动画效果,我也不知道该怎么做。这两个动作的动画方式与自动布局相同。根据我读到的内容,它应该可以工作,并且适用于自动布局中的任何其他动画,排除视图调整大小

想法/评论?我到底做错了什么

我目前的执行情况:

#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

看起来您的动画工作正常,只是字体大小与较小的字体对齐。您强制标签的宽度和高度与使用autoMatchDimension时相同,并且您已请求setAdjustsFontSizeToFitWidth:YES。因此,当计算新布局时,字体大小会减小或增大,这取决于它是在设置动画还是在设置动画。字体大小不是可设置动画的属性,因此更改会立即发生


您可能有幸没有更改图幅高度,而是为视图的比例和图幅原点的更改设置动画。这将确保它变小,但实际上不需要设置文本属性更改的动画。

好的,但缩放和帧动画在自动布局中的效果如何?我可以不用它,但在这个项目中,我想在任何地方都使用自动布局。1尝试设置self.betterPlaceholder.contentMode=UIViewContentModeCenter;或2将您的self.betterPlaceholder添加到UIView-并设置此UIView的帧/大小的动画