ios UIView animateWithDuration 学习

本文介绍了一个iOS应用中视图自动隐藏的实现方法。通过UIView的animateWithDuration方法,结合按钮的位置变化,使得按钮能在滑动过程中平滑地隐藏。此方案适用于需要优雅过渡效果的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下代码实现的效果:下滑到某一高度后自动隐藏掉

原始效果1


原始效果2  慢慢的下滑



原始效果3 隐藏消失


#pragma mark -- UIView animateWithDuration
- (void)initTestBtn
{
    _testBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 230, kDEVICEWIDTH-30, 45)];
    _testBtn.backgroundColor = [UIColor grayColor];
    [_testBtn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
    [_testBtn setTitle:@"提交" forState:UIControlStateNormal];
    _testBtn.titleLabel.textColor = [UIColor whiteColor];
    _testBtn.layer.cornerRadius = 4;
    [self.view addSubview:_testBtn];
    
//    _testView = [[UIView alloc]initWithFrame:CGRectMake(15, 300, kDEVICEWIDTH-30, 45)];
//    _testView.backgroundColor = [UIColor grayColor];
//    
//    [self.view addSubview:_testView];
    
   // [_testView addSubview:_testBtn];
}

- (void)testAnimateWithDuration
{
    /*typeof(self) 是获取到self的类型,这样定义出的weakSelf就是和self一个类型的, 加上__weak是建立一个若引用,整句就是给self定义了一个若引用性质的替身;
    这个一般用在使用block时会用到,因为block会copy它内部的变量,可能会造成引用循环,使用__weak性质的self替代self,可以切断block对self的引用,避免循环引用*/
    __weak __typeof(self) weakSelf = self;
    
    CGRect _tempCG = _testBtn.frame;
    
    _tempCG.origin.y = 360;
    
    [UIView animateWithDuration:1.5
                     animations:^{
                         
                         //_testBtn.frame = CGRectMake(15, kDEVICEHEIGHT, kDEVICEWIDTH-30, 45);
                         
                         _testBtn.frame = _tempCG;
                     }
                     completion:^(BOOL finished)
                     {
                         _testBtn.hidden = YES;
                     }];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值