在做這個功能之前我其實是拒絕的,為什么這么說呢,因為我怕麻煩啊!(開玩笑的,怕麻煩就不做程序員了)
很久沒有寫博客,這次剛好項目中有個有趣的東西,想拿出來分享一下,希望能幫到某些小伙伴。
首先說說需求,是這樣的:
在tableview滑動的過程中,右側的滑動條中間放一個label隨着滑動條一起滑動,並且它的centerY與滑動條的centerY相等。當然,在滑到頂部或者尾部的時候label不能超出tableview的范圍。
然后label上面顯示與之對應的cell的內容。
剛開始想用系統提供的滑動條,scrollIndicator,沒啥用啊。就提供了一個方法,沒辦法完成這種效果,只能自定義一個。下面有幾個變量,我先設置一下,height:tableview的高度,allHeight:tableview內容的高度,y:tableview的contentoffset.y,currentHeight:滑動條的高度。
1.猜想,currentHeight/height=height/allHeight,然后去證實這一猜想,結果是有幾個像素的偏差,不過並不影響。
2.確定滑動塊的位置,要判斷畫框在畫布的上半部分還是下半部分,在上面用top,在下面用bottom(沒看明白繼續往下看,有完整的代碼):
if (y+height/2.0<=allHeight/2.0) { myScrollViewIndirect.top = 66+y*height/allHeight; if (myScrollViewIndirect.top < 66) { myScrollViewIndirect.top = 66; } } else { CGFloat y1 = (allHeight-height-y); myScrollViewIndirect.bottom = 62+height-y1*height/allHeight; if (myScrollViewIndirect.bottom>62+height) { myScrollViewIndirect.bottom = 62+height; } }
3.滑塊的位置的高度確定了,相當於已經完成了一半,接下來是確定label的位置,這就簡單了,將滑動條的centerY給label的centerY就可以了。不過滑到最頂部,而且繼續往下拉,滑動到最底部繼續往上拉的時候需要判斷一下label的位置。很簡單的條件語句,后面有。
4.最核心的功能,label顯示其所在位置的cell內容。通過遍歷tableview里面cell的位置來判斷對應的是哪一個cell,獲取其下標,然后在數據源中得到內容。不過由於是遍歷,我實在沒臉單獨把這個算法拿出來,想想以后還是學學高級一點的算法吧。
最后附上代碼,不然就要被噴了。
1 #define SUBCOUNT 0.2 2 #import "ALDayPictureWallViewController.h" 3 #import "ALGoalPictureWallViewController.h" 4 5 @interface ALDayPictureWallViewController ()<UITableViewDelegate,UITableViewDataSource> 6 { 7 NSTimer *timer; 8 CGFloat subCount; 9 CGFloat y; 10 //自定義滑動塊 11 UIView *myScrollViewIndirect; 12 UILabel *myLabel; 13 NSInteger cellCount; 14 } 15 @property (nonatomic, strong) UITableView *tableView; 16 @property (nonatomic, strong) NSMutableArray *dataArray; 17 18 @end 19 20 @implementation ALDayPictureWallViewController 21 22 - (void)viewDidLoad { 23 [super viewDidLoad]; 24 // Do any additional setup after loading the view. 25 self.view.backgroundColor = [UIColor whiteColor]; 26 subCount = SUBCOUNT; 27 [self createNavi]; 28 [self createUI]; 29 30 myScrollViewIndirect = [LMFUITools createView:CGRectMake(0, 64, 3, 0) andBackgroundColor:[UIColor grayColor]]; 31 myScrollViewIndirect.layer.cornerRadius = myScrollViewIndirect.width/2.0; 32 myScrollViewIndirect.right = SCREEN_WIDTH-3; 33 [self.view addSubview:myScrollViewIndirect]; 34 35 myLabel = [LMFUITools createLabelWithFrame:CGRectMake(0, 0, 100, 0) andBackgroundColor:[UIColor cyanColor] andTextAlignment:NSTextAlignmentCenter andFont:[UIFont systemFontOfSize:17] andText:@"" andTextColor:[UIColor blackColor]]; 36 myLabel.right = myScrollViewIndirect.left-2; 37 [self.view addSubview:myLabel]; 38 39 [self loadData]; 40 41 } 42 43 - (void)loadData 44 { 45 for (int i=0; i<40; i++) 46 { 47 NSString *str = [NSString stringWithFormat:@"第%i行", i]; 48 [self.dataArray addObject:str]; 49 } 50 [self.tableView reloadData]; 51 } 52 53 - (NSMutableArray *)dataArray 54 { 55 if (_dataArray == nil) 56 { 57 _dataArray = [NSMutableArray array]; 58 } 59 return _dataArray; 60 } 61 62 - (void)createNavi 63 { 64 float height = 20; 65 float width = height * 16 / 28; 66 UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(6, 20, 60, 44)]; 67 [self.view addSubview:leftView]; 68 UIImageView *image = [LMFUITools createImageView:CGRectMake(10, (44-height)/2, width, height) andBackgroundImage:@"naviBlack_image"]; 69 [leftView addSubview:image]; 70 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goBackView:)]; 71 [leftView addGestureRecognizer:tap]; 72 73 UILabel *titleLable = [LMFUITools createLabelWithFrame:CGRectMake(0, 20, SCREEN_WIDTH/2.0, 44) andBackgroundColor:nil andTextAlignment:NSTextAlignmentCenter andFont:[UIFont systemFontOfSize:17] andText:@"照片牆" andTextColor:[UIColor blackColor]]; 74 titleLable.centerX = SCREEN_WIDTH * 0.5; 75 [self.view addSubview:titleLable]; 76 77 //平鋪 78 UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)]; 79 rightView.right = SCREEN_WIDTH; 80 rightView.top = 20; 81 [self.view addSubview:rightView]; 82 83 UIImageView *rightImage = [LMFUITools createImageView:CGRectMake(0, 0, 44, 44) andBackgroundImage:@""]; 84 [rightView addSubview:rightImage]; 85 UILabel *label = [LMFUITools createLabelWithFrame:CGRectMake(0, 0, 44, 44) andBackgroundColor:nil andTextAlignment:NSTextAlignmentLeft andFont:[UIFont systemFontOfSize:15] andText:@"平鋪" andTextColor:[UIColor colorWithHexString:@"#999999"]]; 86 label.left = rightImage.right; 87 [rightView addSubview:label]; 88 89 UITapGestureRecognizer *goalPictureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startTimer)]; 90 [rightView addGestureRecognizer:goalPictureTap]; 91 } 92 93 - (void)startTimer 94 { 95 if (timer == nil) 96 { 97 timer = [NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:@selector(startScrollTableView) userInfo:nil repeats:YES]; 98 self.tableView.userInteractionEnabled = NO; 99 [timer fire]; 100 } 101 else 102 { 103 [timer setFireDate:[NSDate distantFuture]]; 104 timer = nil; 105 [timer invalidate]; 106 self.tableView.userInteractionEnabled = YES; 107 } 108 } 109 110 - (void)createUI 111 { 112 self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain]; 113 self.tableView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"]; 114 // self.tableView.showsVerticalScrollIndicator = NO; 115 // self.tableView.showsHorizontalScrollIndicator = NO; 116 // self.tableView.separatorInset = UIEdgeInsetsZero; 117 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 118 self.tableView.delegate = self; 119 self.tableView.dataSource = self; 120 [self.view addSubview:self.tableView]; 121 [self.tableView setContentOffset:CGPointMake(0, 0)]; 122 } 123 124 #pragma mark --tableview delegate 125 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 126 { 127 return 44; 128 } 129 130 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 131 { 132 return [self.dataArray count]; 133 } 134 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 135 { 136 return 1; 137 } 138 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 139 { 140 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 141 if (cell == nil) 142 { 143 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 144 } 145 if ([self.dataArray count]) 146 { 147 cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row]; 148 } 149 150 cell.selectionStyle = 0; 151 return cell; 152 } 153 154 - (void)goalPicture:(UITapGestureRecognizer *)gesture 155 { 156 ALGoalPictureWallViewController *goalPictureView = [[ALGoalPictureWallViewController alloc] init]; 157 self.hidesBottomBarWhenPushed = YES; 158 [self.navigationController pushViewController:goalPictureView animated:YES]; 159 } 160 161 #pragma mark --開始滑動 162 - (void)startScrollTableView 163 { 164 y+=subCount; 165 NSInteger count = 40; 166 CGFloat height = count*44.0; 167 if (y>height-self.tableView.height) 168 { 169 subCount = -SUBCOUNT; 170 } 171 else if (y<0) 172 { 173 subCount = SUBCOUNT; 174 } 175 [self.tableView setContentOffset:CGPointMake(0, y)]; 176 } 177 178 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 179 { 180 NSLog(@"contentOffset.y %f", scrollView.contentOffset.y); 181 y = scrollView.contentOffset.y; 182 183 //需要tableview的contentoffset.y,tableview的高度,tableview畫布的高度 184 CGFloat height = scrollView.height; 185 CGFloat allHeight = scrollView.contentSize.height; 186 187 //計算自定義滑塊的高度 188 myScrollViewIndirect.height = height*height/allHeight; 189 //控件的高度不能設為負數,它會自動轉化為正數,阿西吧 190 float currentHeight = myScrollViewIndirect.height; 191 //計算滑塊 192 //如果tableview的中心在畫框的上部,通過top確定滑塊的位置 193 194 if (y<0) 195 { 196 currentHeight += y; 197 } 198 if (y+height>allHeight) 199 { 200 CGFloat h = fabs(y+height-allHeight); 201 currentHeight -= h; 202 } 203 204 if (currentHeight<8) 205 { 206 myScrollViewIndirect.height = 8; 207 } 208 else 209 { 210 myScrollViewIndirect.height = currentHeight; 211 } 212 213 if (y+height/2.0<=allHeight/2.0) 214 { 215 myScrollViewIndirect.top = 66+y*height/allHeight; 216 if (myScrollViewIndirect.top < 66) 217 { 218 myScrollViewIndirect.top = 66; 219 } 220 } 221 else 222 { 223 CGFloat y1 = (allHeight-height-y); 224 myScrollViewIndirect.bottom = 62+height-y1*height/allHeight; 225 if (myScrollViewIndirect.bottom>62+height) 226 { 227 myScrollViewIndirect.bottom = 62+height; 228 } 229 } 230 myLabel.height = 20; 231 myLabel.centerY = myScrollViewIndirect.centerY; 232 if (myLabel.top < 66) 233 { 234 myLabel.top = 66; 235 } 236 if (myLabel.bottom>62+height) 237 { 238 myLabel.bottom = 62+height; 239 } 240 CGFloat centerY = myLabel.centerY+y-64; 241 for (NSInteger i=0; i<[self.dataArray count]; i++) 242 { 243 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 244 CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath]; 245 // CGRect rect = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]]; 246 if (rectInTableView.origin.y<=centerY && centerY<=rectInTableView.origin.y+rectInTableView.size.height) 247 { 248 NSLog(@"cell.y=%f cellHeight=%f", rectInTableView.origin.y, rectInTableView.size.height); 249 myLabel.text = [self.dataArray objectAtIndex:i]; 250 return; 251 } 252 if (rectInTableView.origin.y > centerY) 253 { 254 NSLog(@"cell.y=%f cellHeight=%f", rectInTableView.origin.y, rectInTableView.size.height); 255 return; 256 } 257 } 258 } 259 260 - (void)goBackView:(UITapGestureRecognizer *)gesture 261 { 262 [self.navigationController popViewControllerAnimated:YES]; 263 } 264 265 - (void)viewWillAppear:(BOOL)animated 266 { 267 [super viewWillAppear:animated]; 268 self.navigationController.navigationBar.hidden = YES; 269 } 270 271 - (void)viewWillDisappear:(BOOL)animated 272 { 273 [super viewWillDisappear:animated]; 274 [timer setFireDate:[NSDate distantFuture]]; 275 timer = nil; 276 [timer invalidate]; 277 self.navigationController.navigationBar.hidden = NO; 278 } 279 280 - (void)didReceiveMemoryWarning { 281 [super didReceiveMemoryWarning]; 282 // Dispose of any resources that can be recreated. 283 } 284 285 @end
