蘋果開發之iOS加入購物車動畫效果

來源:互聯網
上載者:User
 代碼如下 複製代碼
#pragma mark -購物籃按鈕點擊事件
-(void)shoppingCartButtonAction:(UIButton*)sender
{
    if ([[userDef objectForKey:@"id"]isEqualToString:@"0"]) {
        [self showHUDTextOnly:@"請登入後,再進行操作" ];
        return;
    }
    NSDictionary *parameters = @{
                                 @"user_id":[userDef objectForKey:@"id"],
                                 @"gp_id":[NSString stringWithFormat:@"%@",[cell2Array[sender.tag - 101] objectForKey:@"gp_id"]],
                                 @"number" :@1
                                 };
    [self defaultRequestwithURL:KAddToCartURL withParameters:parameters withMethod:@"POST" withBlock:^(NSDictionary *dict, NSError *error){
         if ([[dict objectForKey:kCode] integerValue]==1) {
             //該部分動畫 以self.view為參考系進行
             UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"button_hot_shopping-cart_red-1"]];
             imageView.contentMode=UIViewContentModeCenter;
             imageView.frame = sender.bounds;
             imageView.hidden=YES;
             CGPoint point= sender.frame.origin;
             imageView.center=point;
             CALayer *layer=[[CALayer alloc]init];
             layer.contents=imageView.layer.contents;
             layer.frame=imageView.frame;
             layer.opacity=1;
             [self.view.layer addSublayer:layer];
             //動畫 終點 都以sel.view為參考系
             CGPoint endpoint= CGPointMake(SCREEN_WIDTH*7/10.0, SCREEN_HEIGHT - 22);
             UIBezierPath *path=[UIBezierPath bezierPath];
             //動畫起點
             CGRect rect=[sender convertRect: sender.bounds toView:self.view];
             CGPoint startPoint=CGPointMake(rect.origin.x +rect.size.width/2, rect.origin.y +rect.size.height/2);
             [path moveToPoint:startPoint];
             //貝茲路徑中間點
             float sx=startPoint.x;
             float sy=startPoint.y;
             float ex=endpoint.x;
             float ey=endpoint.y;
             float x=sx+(ex-sx)/3;
             float y=sy+(ey-sy)*0.5-400;
             CGPoint centerPoint=CGPointMake(x,y);
             [path addQuadCurveToPoint:endpoint controlPoint:centerPoint];
             
             CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
             animation.path = path.CGPath;
             animation.removedOnCompletion = NO;
             animation.fillMode = kCAFillModeForwards;
             animation.duration=0.8;
             animation.delegate=self;
             animation.autoreverses= NO;
             animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
             [layer addAnimation:animation forKey:@"buy"];
             //更新角標
             [self cornerNumber];
         }else {
             [self showHUDTextOnly:[dict objectForKey:kMessage]];
        }
     }];
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.