iOS UITableView頭視圖下拉放大效果及UITableviewCell滑動(dòng)顯示多個(gè)按鈕

很多app的在有關(guān)于用戶信息的頁面中勃教,都會(huì)有頭部下拉放大的效果兄墅,類似QQ空間一樣。還有想微信的聊天列表中晃酒,左滑會(huì)出現(xiàn)刪除的按鈕表牢。今天呢和大家分享一下如何去實(shí)現(xiàn)這些小功能。

首先贝次,我們先創(chuàng)建一個(gè)tableview

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)UIImageView *headerBackView;
@property(nonatomic, strong)UIImageView *photoImageView;
@property(nonatomic, strong)UILabel *userNameLabel;
@property(nonatomic, strong)UILabel *introduceLabel;
@property(nonatomic, strong)UIView *tableViewHeaderView;
@property(nonatomic, assign)NSInteger imageHeight;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
 
    _dataSourceArray = [NSMutableArray arrayWithObjects:@"誰念西風(fēng)獨(dú)自涼,蕭蕭黃葉閉疏窗,沉思往事立殘陽",@"被酒莫驚春睡重,賭書消得潑茶香,當(dāng)時(shí)只道是尋常",@"等閑變卻故人心,卻道故人心易變",@"誰念西風(fēng)獨(dú)自涼,蕭蕭黃葉閉疏窗,沉思往事立殘陽",@"被酒莫驚春睡重,賭書消得潑茶香,當(dāng)時(shí)只道是尋常",@"等閑變卻故人心,卻道故人心易變",nil];
 
}
-(void)loadView{
    [super loadView];
     _imageHeight = 160;//背景圖片的高度
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [self createTableViewHeaderView];
    
}

下拉放大效果

下拉放大效果崔兴,主要是將背景圖放在TableView的頭視圖上,通過下拉的距離從而去改變背景圖的frame

創(chuàng)建頭視圖

-(void)createTableViewHeaderView{
    

    _tableViewHeaderView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenWidth, _imageHeight))];
    _headerBackView = [[UIImageView alloc] init];
    
//    背景圖
    _headerBackView.frame = CGRectMake(0, 0, ScreenWidth, _imageHeight);
    _headerBackView.image = [UIImage imageNamed:@"bj1@2x.jpg"];
 
    [_tableViewHeaderView addSubview:_headerBackView];
    _photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 62)/2, 15 , 62 , 62 )];
    [self.tableViewHeaderView addSubview:self.photoImageView];
    _photoImageView.layer.cornerRadius = 31 ;
    _photoImageView.layer.masksToBounds = YES;
    
    _photoImageView.image = [UIImage imageNamed:@"2.jpg"];
    
    _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _photoImageView.frame.origin.y + _photoImageView.frame.size.height + 8 , ScreenWidth, 20 )];
    _userNameLabel.font = [UIFont fontWithName:@"iconfont" size:16 ];
    _userNameLabel.text = @"納蘭性德";
    _userNameLabel.textAlignment = 1;
    _userNameLabel.font = [UIFont systemFontOfSize:16  ];
    _userNameLabel.textColor = [UIColor whiteColor];
    [_tableViewHeaderView addSubview:self.userNameLabel];
   
    _introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth - 229 )/2, _userNameLabel.frame.origin.y + _userNameLabel.frame.size.height + 10 , 229 , 16 )];
    _introduceLabel.alpha = .7;
    _introduceLabel.text = @"人生若只如初見蛔翅,何事秋風(fēng)悲畫扇";
    _introduceLabel.textAlignment = 1;
    _introduceLabel.font = [UIFont systemFontOfSize:12 ];
    _introduceLabel.textColor = _userNameLabel.textColor;
    [_tableViewHeaderView addSubview:self.introduceLabel];
   
    self.tableView.tableHeaderView = _tableViewHeaderView;
 
}

接下來敲茄,就是實(shí)現(xiàn)下拉放大功能了,在- (void)scrollViewDidScroll:(UIScrollView *)scrollView方法里做相應(yīng)的操作

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    CGFloat width = self.view.frame.size.width; // 圖片寬度
    CGFloat yOffset = scrollView.contentOffset.y;  // 偏移的y值
    if (yOffset < 0) {
        CGFloat totalOffset = _imageHeight + ABS(yOffset);
        CGFloat f = totalOffset / _imageHeight;
        self.headerBackView.frame =  CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset); //拉伸后的圖片的frame應(yīng)該是同比例縮放搁宾。
    }
    
 
}

這樣就達(dá)到了我們想要的下拉放大效果

滑動(dòng)UITableViewCell顯示多個(gè)按鈕

這個(gè)主要通過UITableViewRowAction這個(gè)類去實(shí)現(xiàn)

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath這個(gè)方法里面的代碼只在iOS8.0以前版本有作用折汞,也可以不實(shí)現(xiàn)。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;//此處的EditingStyle可等于任意UITableViewCellEditingStyle盖腿,該行代碼只在iOS8.0以前版本有作用爽待,也可以不實(shí)現(xiàn)。
}

設(shè)置相應(yīng)的按鈕翩腐。通過UITableViewRowAction來實(shí)現(xiàn)對應(yīng)的按鈕及按鈕的響應(yīng)事件鸟款,按鈕的排列順序可以通過更換按鈕的響應(yīng)事件在數(shù)組中的位置來調(diào)整

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath


{
 
    //設(shè)置刪除按鈕
    
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
//        更新數(shù)據(jù)
        [self.dataSourceArray removeObjectAtIndex:indexPath.row];
        
//        更新UI
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    
    //設(shè)置收藏按鈕
    
    UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        
        collectRowAction.backgroundColor = [UIColor greenColor];
        
        //實(shí)現(xiàn)收藏功能
        NSLog(@"收藏成功");
        
    }];
    
    //設(shè)置置頂按鈕
    
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        [self.dataSourceArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
       
        [_tableView reloadData];
        
    }];
    
// 設(shè)置按鈕的背景顏色
    topRowAction.backgroundColor = [UIColor blueColor];
 
    collectRowAction.backgroundColor = [UIColor grayColor];
 
    return  @[deleteRowAction,collectRowAction,topRowAction];//可以通過調(diào)整數(shù)組的順序而改變按鈕的排序
    
 
}

這樣滑動(dòng)顯示按鈕的效果就實(shí)現(xiàn)了。不過UITableViewRowAction這個(gè)類只有在iOS8以后才能用

界面如下:

界面圖

GIF動(dòng)圖如下:

下拉放大與滑動(dòng)顯示.gif

demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茂卦,一起剝皮案震驚了整個(gè)濱河市何什,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌等龙,老刑警劉巖处渣,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蛛砰,居然都是意外死亡罐栈,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門泥畅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荠诬,“玉大人,你說我怎么就攤上這事位仁「陶辏” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵聂抢,是天一觀的道長钧嘶。 經(jīng)常有香客問我,道長琳疏,這世上最難降的妖魔是什么康辑? 我笑而不...
    開封第一講書人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任摄欲,我火速辦了婚禮,結(jié)果婚禮上疮薇,老公的妹妹穿的比我還像新娘胸墙。我一直安慰自己,他們只是感情好按咒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開白布迟隅。 她就那樣靜靜地躺著,像睡著了一般励七。 火紅的嫁衣襯著肌膚如雪智袭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評(píng)論 1 299
  • 那天掠抬,我揣著相機(jī)與錄音吼野,去河邊找鬼。 笑死两波,一個(gè)胖子當(dāng)著我的面吹牛瞳步,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播腰奋,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼单起,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了劣坊?” 一聲冷哼從身側(cè)響起嘀倒,我...
    開封第一講書人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎局冰,沒想到半個(gè)月后测蘑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡康二,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年碳胳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赠摇。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖浅蚪,靈堂內(nèi)的尸體忽然破棺而出藕帜,到底是詐尸還是另有隱情,我是刑警寧澤惜傲,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布洽故,位于F島的核電站,受9級(jí)特大地震影響盗誊,放射性物質(zhì)發(fā)生泄漏时甚。R本人自食惡果不足惜隘弊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望荒适。 院中可真熱鬧梨熙,春花似錦、人聲如沸刀诬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陕壹。三九已至质欲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間糠馆,已是汗流浹背嘶伟。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留又碌,地道東北人九昧。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像赠橙,于是被迫代替她去往敵國和親耽装。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,755評(píng)論 22 665
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫期揪、插件掉奄、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評(píng)論 4 62
  • 晚上下了班,換了件露肩的黑色連衣裙凤薛,畫了個(gè)簡單的淡妝姓建,將盤起的頭發(fā)隨意披散下來。 “瀟瀟缤苫,你平時(shí)就該多打扮自己速兔,別...
    奧利奧與甜甜圈閱讀 180評(píng)論 0 0
  • 隨著各種聊天工具和平臺(tái)的發(fā)展,我們也有越來越多的機(jī)會(huì)聽到一些大牛的分享活玲。不再像以前那樣得花費(fèi)大量的人力物力涣狗。記得大...
    蘇老夫子閱讀 1,248評(píng)論 5 33
  • 親子閱讀每一天,11月1日今天我們一起閱讀了《愛打岔的小雞》舒憾,這是一本非常有趣的繪本镀钓,那只可愛的小紅雞,還有很無奈...
    亭子?jì)屵?/span>閱讀 1,289評(píng)論 0 0