頁控件默認(rèn)是無法定位點(diǎn)擊的,不能實(shí)現(xiàn)當(dāng)前頁為首頁時(shí)丹皱,點(diǎn)擊跳轉(zhuǎn)到末尾的效果鲤脏。
頁控件需要了解的一些信息:
- 默認(rèn)小圓點(diǎn)的矩陣寬高為7px左右
- 小圓點(diǎn)之間的空白間距為9px左右
在頁控件上覆蓋同等數(shù)量的透明按鈕累盗,實(shí)現(xiàn)精確定位的點(diǎn)擊效果,類似湯姆貓點(diǎn)擊圖片的頭部蹋半,手,腳等部位會(huì)產(chǎn)生相應(yīng)動(dòng)畫充坑,原理也是在圖片上添加透明的按鈕减江。
01. // 注意UIButton按鈕是添加到self.view上的,而不是UIPageControl捻爷,試了下辈灼,UIPageControl添加不了子視圖
02. for(int i = 0; i < ... ; i++)
03. {
04. //循環(huán)添加按鈕
05. UIButton *button = [[UIButton alloc] init];
06. button.center = CGPointMake(小圓點(diǎn)的中心點(diǎn).x,小圓點(diǎn)的中心點(diǎn).y);
07. button.bounds = CGRectMake(0,0,8,8); //略大于小圓點(diǎn)的矩形
08. button.backgoundColor = [UIColor clearColor];
09. // 設(shè)置按鈕的tag值,用于標(biāo)識按鈕
10. button.tag = i;
11. [self.view addSubview:button];
12. // 對按鈕添加監(jiān)聽也榄,監(jiān)聽UIControlEventTouchDown事件
13. [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchDown];
14. //對按鈕繼續(xù)添加監(jiān)聽巡莹,監(jiān)聽UIControlEventTouchUpInside事件
15. [button addTarget:self action:手指離開時(shí),重啟定時(shí)器 forControlEvents:UIControlEventTouchUpInside];
16. }
17. ...
實(shí)現(xiàn)手指點(diǎn)擊按鈕時(shí)甜紫,停止定時(shí)器降宅,并且切換圖片,至于頁控件的當(dāng)前頁碼值會(huì)跟隨圖片的切換自動(dòng)更改棵介,之前在自動(dòng)輪播器中設(shè)置過钉鸯。
01. ...
02. - (void)pressButton:(UIButton *)button
03. {
04. //獲取按鈕的標(biāo)識值作為頁碼
05. NSInteger page = button.tag;
06. [self.scrollview setContentOffset:CGSizeMake(page * self.scrollview.frame.size.width, 0);
07. // 頁控件的當(dāng)前頁碼設(shè)置是在監(jiān)聽滾動(dòng)視圖滾動(dòng)時(shí)實(shí)現(xiàn)的
08. }
09. ...