iOS 项目中用到的拖拽界面动画

本文介绍了一个iOS应用中的视图拖动与UITableView联动的实现方式。通过UIPanGestureRecognizer处理视图拖动,并根据拖动位置显示或隐藏UITableView。此外,还实现了UITableView的数据源和代理方法,以展示固定数量的单元格。

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

#define WIDTH     UIScreen.mainScreen.bounds.size.width
#define HEIGHT    UIScreen.mainScreen.bounds.size.height

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIView *_backView;
}
@end

@implementation ViewController
- (void)dealloc {
    
}
- (void)viewDidLoad {
    [superviewDidLoad];
    self.view.backgroundColor = [UIColorredColor];
    
    UIView *backView = [[UIViewalloc] initWithFrame:CGRectMake(0, HEIGHT - 50,WIDTH, HEIGHT +50)];
    backView.backgroundColor = [UIColororangeColor];
    [self.viewaddSubview:backView];
    UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizeralloc] initWithTarget:self action:@selector(doHandlePanAction:)];
    [backView addGestureRecognizer:panGestureRecognizer];
    _backView = backView;
    
    
    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
    btn.frame =CGRectMake(WIDTH/2-50,0,100, 50);
    btn.backgroundColor = [UIColorcyanColor];
    [backView addSubview:btn];

    
    UITableView * tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,50, WIDTH,HEIGHT) style:UITableViewStylePlain];
    tableView.delegate =self;
    tableView.dataSource =self;
    [backView addSubview:tableView];
    
    [tableView addObserver:selfforKeyPath:@"contentOffset"options:NSKeyValueObservingOptionNewcontext:nil];
    

}
- (void)doHandlePanAction:(UIPanGestureRecognizer *)paramSender{
    
    CGPoint point = [paramSendertranslationInView:self.view];
    NSLog(@"X:%f;Y:%f",point.x,point.y);
//    paramSender.view.center = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y);
    paramSender.view.center =CGPointMake(paramSender.view.center.x, paramSender.view.center.y + point.y);
    [paramSender setTranslation:CGPointMake(0,0) inView:self.view];
    if ( paramSender.state ==UIGestureRecognizerStateEnded) {
        NSLog(@"%f,%f",_backView.frame.origin.y,HEIGHT/4 *3);
        if (_backView.frame.origin.y > HEIGHT/4 *3) {
            [selftableViewShow:NO];
        }else {
            [selftableViewShow:YES];
        }
    }
}
- (void)tableViewShow:(BOOL)show {
    [UIViewanimateWithDuration:0.3animations:^{
        _backView.frame =CGRectMake(0, show?-50:HEIGHT-50,WIDTH, HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return30;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str =@"cellIde";
    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];
    }
    return cell;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPathisEqualToString:@"contentOffset"]) {
        CGPoint offset = [change[NSKeyValueChangeNewKey]CGPointValue];
        NSLog(@" ===== offset ====%f",offset.y);
        if (offset.y < -20) {
            [selftableViewShow:NO];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值