美文网首页
iOS BarButtonItem在navigationCont

iOS BarButtonItem在navigationCont

作者: 温柔vs先生 | 来源:发表于2018-04-04 16:04 被阅读0次
今天遇到一个需求 视频.gif

我们要的效果是点击下面加入购物车后生成一个动画然后跳到导航栏中的购物车中,然后就在点击时做了以下操作:

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];
            }];

可以看到我是先创建了一张新的图片让她进行动画,结束后隐藏。而上面GIF图上出现的结果是因为imagview.frame = frameInNaviView;这句代码导致的,而frameInNaviView就是我获取到的导航栏中的购物车的坐标,我先说一下我是怎么回去他的坐标的:

- (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];

}

没错,就是在导航栏中添加完UIBarButtonItem时 我获取了他在self.navigationController.view中的位置坐标,但是却出现了上面这种情况,我打印以后坐标是(0,0,30,30)很奇怪,我又打印cartBtn.superview居然是nil,所以我就想,是不是viewdidload方法没有执行完导致导航栏中的视图没有加载出来。

所以我就在我需要这个位置的地方获取了坐标及:

{
        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;
    }

请看block回调里面,在这里我获取到了他的坐标

最后实现的效果是这样的: 视频1.gif

这里我依然不是特别清楚为什么在创建的时候我获取坐标获取不到,希望看到的大神们能够给我建议,共同进步。

相关文章

网友评论

      本文标题:iOS BarButtonItem在navigationCont

      本文链接:https://www.haomeiwen.com/subject/tankhftx.html