前言
最近項(xiàng)目有一個(gè)需求需要實(shí)現(xiàn)一個(gè)類似氣泡的popover彈窗黔龟,就是點(diǎn)擊一個(gè)按鈕的時(shí)候在按鈕的某一個(gè)方向彈出一個(gè)視圖娃殖,這個(gè)視圖需要帶有一個(gè)箭頭指向捅伤。就像下圖是簡書和微信的樣式
簡書 系統(tǒng)樣式
|
微信 自定義樣式
|
---|
系統(tǒng)樣式UIPopoverPresentationController
系統(tǒng)樣式就比較簡單了对竣,就是一個(gè)UIViewController然后設(shè)置modalPresentationStyle屬性,要記住實(shí)現(xiàn)UIPopoverPresentationControllerDelegate代理的adaptivePresentationStyleForPresentationController方法
具體看代碼
- (IBAction)clickpopoverBtn:(id)sender {
PopViewController *testVC = [[PopViewController alloc] init];
testVC.preferredContentSize = CGSizeMake(150, 150);
testVC.modalPresentationStyle = UIModalPresentationPopover;
testVC.popoverPresentationController.delegate = self;
testVC.popoverPresentationController.sourceView = self.popoverBtn;
testVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
testVC.popoverPresentationController.backgroundColor = [UIColor redColor];
testVC.popoverPresentationController.canOverlapSourceViewRect = NO;
[self presentViewController:testVC animated:YES completion:nil];
}
#pragma mark - <UIPopoverPresentationControllerDelegate>
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
popover主動(dòng)消失捏卓,直接調(diào)用dismiss就可以了极祸。
- (void)clickBtn:(UIButton *)aBtn {
[self dismissViewControllerAnimated:YES completion:nil];
}
但是有個(gè)的地方需要注意,滑動(dòng)觸摸顯示區(qū)域以外的區(qū)域是不會(huì)消失怠晴,只有
才可以消失遥金。這個(gè)點(diǎn)很僵硬,萬惡的產(chǎn)品是很難接受的蒜田!
還有就是箭頭的大小稿械,間距,顯示隱藏的動(dòng)畫時(shí)間點(diǎn)等都難修改冲粤,不過我個(gè)人覺得系統(tǒng)自帶的控件本身就挺好看的美莫。。梯捕。厢呵。。
自定義樣式
系統(tǒng)自帶的不合適就只能自己擼羅傀顾。襟铭。。
1.使用了一個(gè)三角形的圖片做箭頭短曾,支持上下左右蝌矛;
2.顯示內(nèi)容contentView暴露出來是可以添加其他的UIView
3.contentView支持跳轉(zhuǎn)大小等。错英。。
4.支持顯示區(qū)域范圍的設(shè)施隆豹,超出范圍會(huì)自動(dòng)移動(dòng)到顯示范圍內(nèi)椭岩。
5.上下會(huì)自動(dòng)調(diào)整,左右會(huì)自動(dòng)調(diào)整璃赡,不支持設(shè)置為Up自動(dòng)調(diào)整為Right判哥。
代碼傳送門