**下拉菜單是非常常用的一個(gè)功能掖桦,這里封裝了一個(gè)自帶動(dòng)畫(huà)下拉菜單 **瘸恼。
- 構(gòu)建方法
/**
* 構(gòu)建方法
* param array 標(biāo)題
* 每行默認(rèn)高度40
*/
-(instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray<NSString *> *)array;
根據(jù)傳入的array來(lái)創(chuàng)建列表,需要注意的是顿膨,每次添加后髓绽,都需要手動(dòng)調(diào)用beginAnimation
方法來(lái)做動(dòng)畫(huà)敛苇。移除時(shí)調(diào)用dismiss
方法,button點(diǎn)擊方法如下如下:
if ([self.view viewWithTag:1])
{
[self.menuView dismiss];
}
else
{
[self.view addSubview:self.menuView];
[self.menuView beginAnimation];
}
點(diǎn)擊使用block來(lái)進(jìn)行回調(diào)
self.menuView.clickBlock = ^(NSInteger tag){
UIAlertAction *action =[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:weakSelf.dataArray[tag] preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:action];
[weakSelf presentViewController:alert animated:YES completion:nil];
};
關(guān)于動(dòng)畫(huà)的實(shí)現(xiàn)
使用UIView動(dòng)畫(huà)usingSpringWithDamping
顺呕,并且根據(jù)每個(gè)動(dòng)畫(huà)時(shí)間根據(jù)tag遞增枫攀,使其有彈的效果,代碼如下:
[UIView animateWithDuration:0.3 + 0.05 * idx
delay:0.1
usingSpringWithDamping:0.9
initialSpringVelocity:1
options:UIViewAnimationOptionCurveEaseIn
animations:^{
btn.frame = CGRectMake(10, idx * 40 + 15, 130, 40 - 0.3);
line.frame = CGRectMake(10, idx * 40 + 39.7 + 15, self.bgFrame.size.width - 20, 0.3);
}
completion:^(BOOL finished) {
}];
消失的動(dòng)畫(huà)與開(kāi)始動(dòng)畫(huà)類似
代碼連接gitHub