添加到购物车动画在Swift

时间:2015-02-07 21:39:11

标签: xcode user-interface swift animation

我使用下面的代码执行“添加到购物车动画”, 我最近使用swift构建了一个新的应用程序,我很难将此代码从Objective C转换为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];
}

这是我的快捷代码

     //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(基本,forKey: “变换”)

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

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

XMLStreamWriter不是正确答案。确保正确给出图层中动画的键值。我也面临同样的问题并改为CAKeyframeAnimation。我的任务与你不同。

答案 1 :(得分:0)

如果您的代码中缺少QuartzCore标头的导入,则会出现该错误。所以你需要导入QuartzCore框架:

import QuartzCore

答案 2 :(得分: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");