我們要的效果是點(diǎn)擊下面加入購(gòu)物車(chē)后生成一個(gè)動(dòng)畫(huà)然后跳到導(dǎo)航欄中的購(gòu)物車(chē)中衰伯,然后就在點(diǎn)擊時(shí)做了以下操作:
UIImageView *imagview=[[UIImageView alloc] initWithFrame:cellBtnRect];
imagview.image = [UIImage imageNamed:@"XHAddCartLight"];
[self.navigationController.view addSubview:imagview];
[UIView animateWithDuration:1 animations:^{
imagview.frame = frameInNaviView;
// imagview.alpha = 0;
} completion:^(BOOL finished) {
[imagview removeFromSuperview];
imagview.hidden = YES;
CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
shakeAnimation.duration = 0.25f;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
shakeAnimation.toValue = [NSNumber numberWithFloat:5];
shakeAnimation.autoreverses = YES;
[cartBtn.layer addAnimation:shakeAnimation forKey:nil];
}];
可以看到我是先創(chuàng)建了一張新的圖片讓她進(jìn)行動(dòng)畫(huà),結(jié)束后隱藏笛匙。而上面GIF圖上出現(xiàn)的結(jié)果是因?yàn)閕magview.frame = frameInNaviView;這句代碼導(dǎo)致的,而frameInNaviView就是我獲取到的導(dǎo)航欄中的購(gòu)物車(chē)的坐標(biāo),我先說(shuō)一下我是怎么回去他的坐標(biāo)的:
- (void)addBarButtonItem
{
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame = CGRectMake(0, 0, 27, 27);
[searchBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
[searchBtn setBackgroundImage:[UIImage imageNamed:@"XHSearchImg"] forState:UIControlStateNormal];
UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
cartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cartBtn.frame = CGRectMake(0, 0, 27, 27);
[cartBtn addTarget:self action:@selector(searchClick)
forControlEvents:UIControlEventTouchUpInside];
[cartBtn setBackgroundImage:[UIImage imageNamed:@"XHServiceCart"] forState:UIControlStateNormal];
UIBarButtonItem *cartBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cartBtn];
self.navigationItem.rightBarButtonItems = @[cartBarButtonItem,searchBarButtonItem];
frameInNaviView = [self.navigationController.view convertRect:cartBtn.frame fromView:cartBtn.superview];
}
沒(méi)錯(cuò)折砸,就是在導(dǎo)航欄中添加完UIBarButtonItem時(shí) 我獲取了他在self.navigationController.view中的位置坐標(biāo),但是卻出現(xiàn)了上面這種情況沙峻,我打印以后坐標(biāo)是(0睦授,0,30摔寨,30)很奇怪去枷,我又打印cartBtn.superview居然是nil,所以我就想是复,是不是viewdidload方法沒(méi)有執(zhí)行完導(dǎo)致導(dǎo)航欄中的視圖沒(méi)有加載出來(lái)删顶。
所以我就在我需要這個(gè)位置的地方獲取了坐標(biāo)及:
{
static NSString *identifier = @"identifier";
XHServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"XHServiceTableViewCell" owner:nil options:nil] lastObject];
}
XHServiceGoodsListModel *listModel = _goodsListArr[indexPath.row];
cell.listModel = listModel;
__weak XHServiceTableViewCell *weakCell = cell;
cell.addCartBlock = ^{
__strong XHServiceTableViewCell *strongCell = weakCell;
frameInNaviView = [self.navigationController.view convertRect:cartBtn.frame fromView:cartBtn.superview];
CGRect cellBtnRect = [strongCell convertRect:strongCell.cartBtn.frame toView:self.view];
UIImageView *imagview=[[UIImageView alloc] initWithFrame:cellBtnRect];
imagview.image = [UIImage imageNamed:@"XHAddCartLight"];
[self.navigationController.view addSubview:imagview];
[UIView animateWithDuration:1 animations:^{
imagview.frame = frameInNaviView;
// imagview.alpha = 0;
} completion:^(BOOL finished) {
[imagview removeFromSuperview];
imagview.hidden = YES;
CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
shakeAnimation.duration = 0.25f;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
shakeAnimation.toValue = [NSNumber numberWithFloat:5];
shakeAnimation.autoreverses = YES;
[cartBtn.layer addAnimation:shakeAnimation forKey:nil];
}];
};
return cell;
}
請(qǐng)看block回調(diào)里面,在這里我獲取到了他的坐標(biāo)
這里我依然不是特別清楚為什么在創(chuàng)建的時(shí)候我獲取坐標(biāo)獲取不到淑廊,希望看到的大神們能夠給我建議逗余,共同進(jìn)步。