关于修改LayoutConstraint实现动画

图一
初始实现方法
图二
改良版

第一张图内的代码虽然功能也能实现,但是当这个SwitchListView第一次被父容器调起的时候,动画效果并不是UIView animation内所写的效果。然后被调起过一次之后却又一切正常。(尚还不知道为什么)几番尝试后改成第二张图内的代码就好了。
贴一下最后的代码吧:
#import “SwitchListView.h”

static SwitchListView *instance;

@interface SwitchListView ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;
@property (weak, nonatomic) IBOutlet UILabel *mapLable;
@property (weak, nonatomic) IBOutlet UILabel *textLable;
@end
@implementation SwitchListView

  • (SwitchListView )startWithView:(UIView )superView{
    instance = [[SwitchListView alloc ]initWithFrame:superView.bounds];
    [UIView animateWithDuration:0.1 animations:^{
    [superView addSubview:instance];
    }completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3 animations:^{
    instance.searchTopSpe.constant = 10.;
    [instance layoutIfNeeded];
    }completion:^(BOOL finished) {

    }];
    

    }];
    return instance;
    }

  • (instancetype)initWithFrame:(CGRect)frame{
    if (!instance) {
    self = [super initWithFrame:frame];
    instance = [[NSBundle mainBundle] loadNibNamed:@”SwitchListView” owner:nil options:nil][0];
    instance.frame = frame;
    }
    return instance;
    }

  • (void)layoutSubviews{
    [super layoutSubviews];
    self.mapLable.text = LS(@”map search”);
    self.textLable.text = LS(@”text search”);
    }
    //地图搜索
  • (IBAction)mapSearchClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(seletedSearchType:)]) {
    [self.delegate seletedSearchType:1];
    }
    [self stop];
    }
    //文本搜索
  • (IBAction)textSearchClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(seletedSearchType:)]) {
    [self.delegate seletedSearchType:2];
    }
    [self stop];
    }
  • (IBAction)tap_action:(id)sender {
    [self stop];
    }

  • (void)stop{
    [UIView animateWithDuration:0.3 animations:^{
    self.searchTopSpe.constant = -70.;
    [self layoutIfNeeded];
    } completion:^(BOOL finished) {
    [self removeFromSuperview];
    }];
    }

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值