2015-02-07 54 views
-1

我使用下面的代碼來執行「添加到購物車動畫」, 我最近用swift創建了一個新的應用程序,我很難將這段代碼從Objective C轉換爲Swift 。加入購物車Swift中的動畫

這段代碼是動畫一個UITableView按鈕跳轉到購物車(UItabBar項目)

// AddToCart button (cell Button)  
-(void)AddToCart:(UIButton*)sender { 

    // get the selected index 
    CGPoint center= sender.center; 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.Tableview]; 
    NSIndexPath *indexPath = [self.Tableview indexPathForRowAtPoint:rootViewPoint]; 

    // add to cart 
    [checkoutCart AddItem:SandwichArray[indexPath.row]]; 

    MyCell* cell =(MyCell*)[self.Tableview dequeueReusableCellWithIdentifier:@"Cell"]; 

    // grab the imageview 
    UIImageView *imgV = (UIImageView*)[cell viewWithTag:400]; 

    // get the exact location of image 
    CGRect rect = [imgV.superview convertRect:imgV.frame fromView:nil]; 
    rect = CGRectMake(5, (rect.origin.y*-1)-10, imgV.frame.size.width, imgV.frame.size.height); 

    // create new duplicate image 
    UIImageView *starView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AddItem.png"]]; 
    [starView setFrame:rect]; 
    starView.layer.cornerRadius=5; 
    starView.layer.borderColor=[[UIColor blackColor]CGColor]; 
    starView.layer.borderWidth=1; 
    [self.view addSubview:starView]; 

    // apply position animation 
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    pathAnimation.calculationMode = kCAAnimationPaced; 
    pathAnimation.fillMode = kCAFillModeForwards; 
    pathAnimation.removedOnCompletion = NO; 
    pathAnimation.duration=0.75; 
    pathAnimation.delegate=self; 

    // tabbar Position 
    CGPoint endPoint = CGPointMake(210+rect.size.width/2, 390+rect.size.height/2); 

    CGMutablePathRef curvedPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(curvedPath, NULL, starView.frame.origin.x, starView.frame.origin.y); 
    CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, starView.frame.origin.y, endPoint.x, starView.frame.origin.y, endPoint.x, endPoint.y); 
    pathAnimation.path = curvedPath; 
    CGPathRelease(curvedPath); 

    // apply transform animation 
    CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"]; 
    [basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.25, 0.25, 0.25)]]; 
    [basic setAutoreverses:NO]; 
    [basic setDuration:0.75]; 

    [starView.layer addAnimation:pathAnimation forKey:@"curveAnimation"]; 
    [starView.layer addAnimation:basic forKey:@"transform"]; 

    [starView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.75]; 
    [self performSelector:@selector(reloadBadgeNumber) withObject:nil afterDelay:0.75]; 
} 

這是我的SWIFT代碼

 //AddToCart button (of cell) 
    func AddToCart(sender:UIButton){ 

     // get the selected index 
     var center:CGPoint = sender.center; 
     var rootViewPoint:CGPoint = sender.superview!.convertPoint(center, toView:self.TableView) 
     var indexPath:NSIndexPath = self.TableView!.indexPathForRowAtPoint(rootViewPoint)! 

     // add to cart 
     //ShopingCart.AddItem(item) 

     var cell:Menu_Cell = self.TableView!.dequeueReusableCellWithIdentifier("cell") as Menu_Cell 

     //grab the imageview using cell 
     var imgV:UIImageView = cell.imageView! 

     // get the exact location of image 
     var rect:CGRect = imgV.superview!.convertRect(imgV.frame ,fromView:nil) 
     rect = CGRectMake(5, (rect.origin.y*(-1))-10, imgV.frame.size.width, imgV.frame.size.height); 

     // create new duplicate image 
     var starView:UIImageView = cell.imageView! 
     starView.frame = rect 
     starView.layer.cornerRadius=5; 
     starView.layer.borderWidth=1; 
     self.view.addSubview(starView) 

     // position animation 
//  var pathAnimation:CAKeyframeAnimation = CAKeyframeAnimation.animationWithKeyPath("position") 
     var pathAnimation:CAPropertyAnimation = CAPropertyAnimation(keyPath: "position") 
//  pathAnimation.calculationMode = kCAAnimationPaced 
     pathAnimation.fillMode = kCAFillModeForwards 
     pathAnimation.removedOnCompletion = false 
     pathAnimation.duration=0.75 
     pathAnimation.delegate=self 

     // tab-bar right side item frame-point = end point 
     var endPoint:CGPoint = CGPointMake(210+rect.size.width/2, 390+rect.size.height/2); 

     // animation position animation 
     var curvedPath:CGMutablePathRef = CGPathCreateMutable(); 
     CGPathMoveToPoint(curvedPath, nil, starView.frame.origin.x, starView.frame.origin.y); 
     CGPathAddCurveToPoint(curvedPath, nil, endPoint.x, starView.frame.origin.y, endPoint.x, starView.frame.origin.y, endPoint.x, endPoint.y); 
//  pathAnimation.path = curvedPath; 

     // apply transform animation 
//  var basic:CABasicAnimation = CABasicAnimation.animationWithKeyPath("transform") 
     var basic:CAPropertyAnimation = CAPropertyAnimation(keyPath: "transform") 
//  basic.valueForKeyPath(NSValue.valueWithCATransform3D(CATransform3DMakeScale(0.25, 0.25, 0.25))) 
//  basic.setAutoreverses(false) 
     basic.duration = 0.75 

     starView.layer.addAnimation(pathAnimation,forKey: "curveAnimation") 
     starView.layer.addAnimation(basic,forKey:"transform") 

     starView.removeFromSuperview() 
//  [self performSelector:@selector(reloadBadgeNumber) withObject:nil afterDelay:0.75]; 

我得到錯誤的位置:

starView.layer.addAnimation(pathAnimation,forKey:「curveAnimation」) tarView.layer.addAnimation(basic,for Key:「變形」)

**'-[CAPropertyAnimation _copyRenderAnimationForLayer:]: unrecognized selector sent to instance 0x7fd612c11780'** 

有什麼建議嗎?

+0

這段代碼很容易在swift中重寫。我強烈建議你對目標c和swift之間的基本區別做一些研究。如果我們爲你做這項工作,你不會在快速編程中變得更好。您需要向我們展示一些嘗試,以及您的問題在哪裏。 – Christian 2015-02-08 02:47:25

+0

謝謝,我更新了我的swift代碼嘗試。 – Bar 2015-02-08 11:33:12

回答

0

如果您的代碼中缺少導入的QuartzCore標頭,則會顯示該錯誤。所以你需要導入QuartzCore-framework:

import QuartzCore 
1

import QuartzCore是不正確的答案。確保您的圖層中動畫的關鍵值得到了適當的改進。我也面臨相同的,並改爲CAKeyframeAnimation。我的任務與你不同。

0
 var center:CGPoint = sender.center; 
    var rootViewPoint:CGPoint = sender.superview!.convertPoint(center, toView:self.tableView) 
    var indexPath:NSIndexPath = self.tableView!.indexPathForRowAtPoint(rootViewPoint)! 

    var cell:Cell_3 = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell_3 

var imgV:UITextField = cell.tf_adet!

// get the exact location of image 
    var rect:CGRect = imgV.superview!.convertRect(imgV.frame ,fromView:nil) 
    rect = CGRectMake(rect.origin.x, (rect.origin.y*(-1))-10, imgV.frame.size.width, imgV.frame.size.height); 

    // create new duplicate image 
    var starView:UITextField = cell.tf_adet 
    starView.frame = rect 
    starView.layer.cornerRadius=5; 
    starView.layer.borderWidth=1; 
    self.view.addSubview(starView) 



    // now create a bezier path that defines our curve 
    // the animation function needs the curve defined as a CGPath 
    // but these are more difficult to work with, so instead 
    // we'll create a UIBezierPath, and then create a 
    // CGPath from the bezier when we need it 
    let path = UIBezierPath() 


    // tab-bar right side item frame-point = end point 
    var endPoint:CGPoint = CGPointMake(140+rect.size.width/2, 790+rect.size.height/2); 

    path.moveToPoint(CGPointMake(starView.frame.origin.x, starView.frame.origin.y)) 

    path.addCurveToPoint(CGPoint(x: endPoint.x, y: endPoint.y), 
      controlPoint1: CGPoint(x: endPoint.x, y: starView.frame.origin.y), 
      controlPoint2: CGPoint(x: endPoint.x, y: starView.frame.origin.y)) 



    // create a new CAKeyframeAnimation that animates the objects position 
    let anim = CAKeyframeAnimation(keyPath: "position") 

    // set the animations path to our bezier curve 
    anim.path = path.CGPath 

    // set some more parameters for the animation 
    // this rotation mode means that our object will rotate so that it's parallel to whatever point it is currently on the curve 
    // anim.rotationMode = kCAFillModeForwards 
    anim.fillMode = kCAFillModeForwards 
    //anim.repeatCount = Float.infinity 
    anim.duration = 0.65 
    anim.removedOnCompletion = false 
    anim.delegate=self 


    // apply transform animation 
    var animation : CABasicAnimation = CABasicAnimation(keyPath: "transform"); 
    var transform : CATransform3D = CATransform3DMakeScale(2,2,1) //0.25, 0.25, 0.25); 
    //animation.setValue(NSValue(CATransform3D: transform), forKey: "scaleText"); 
    animation.duration = 0.75; 

    starView.layer.addAnimation(anim, forKey: "curveAnimation") 
    starView.layer.addAnimation(animation, forKey: "transform"); 
+1

您能否添加更多解釋? – DaGardner 2015-08-03 20:41:35