Swift实现弹出菜单

var sp1Btn:UIButton? = nil;
    
    
    var customTimer: Timer? = nil;
    
    
    var testArray:NSMutableArray = NSMutableArray(capacity: 0)
    
    override func viewDidLoad() {
      
        super.viewDidLoad()
        
        
        
        let sp1Btn = UIButton(frame: CGRect(x: 260, y: 100, width: 38, height: 38))
        
        sp1Btn.setImage(UIImage(named: "sp1"), for: UIControlState.normal)
        
        sp1Btn.addTarget(self, action:#selector(btnClick) , for: UIControlEvents.touchUpInside)
        
       
        
        self.sp1Btn = sp1Btn;
        
        let array = ["好友","远见","举报"];
        
       
        
        for i in 0  ..< 3
        {
            let customeBtn: CustomButton = CustomButton(frame: CGRect(x: 260, y: 100, width: 38, height: 38));
            
            customeBtn.setBackgroundImage(UIImage(named: "sp11"), for: UIControlState.normal)
            
            customeBtn.setTitle(array[i], for: UIControlState.normal)
            
            customeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 10)
            
            customeBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
            

            
            self.view.addSubview(customeBtn);
            
            
            customeBtn.startPoint = CGPoint(x:  278.5 , y: 118.5);
            
            let point_x:CGFloat = (CGFloat)(20*i);
            
             let point_y:CGFloat = (CGFloat)(50*i);
            
            
            customeBtn.nearPoint = CGPoint(x:  (sp1Btn.frame.origin.x-90 + point_x ) , y: sp1Btn.frame.origin.y-40+point_y)
            
              customeBtn.endPoint = CGPoint(x:  (sp1Btn.frame.origin.x-90 + point_x) , y: sp1Btn.frame.origin.y-40+point_y)
            
            
            
            
            testArray.add(customeBtn)
       }
        
        
         self.view.addSubview(sp1Btn)
    }
    
    
    func btnClick()
    {
        var systemSoundID:SystemSoundID = 0;
        
        self.sp1Btn?.isSelected = !(self.sp1Btn?.isSelected)!;

        
        let path = Bundle.main.path(forResource: "composer_close", ofType: "wav")
        
        AudioServicesCreateSystemSoundID(NSURL.fileURL(withPath: path!) as CFURL, &systemSoundID)
        
        
        AudioServicesPlayAlertSound(SystemSoundID(systemSoundID))

        
        
        var  i = 0;
        
        var timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (parttimer) in
            
            i+=1;
            
            print("结果",i)
            
            
            
            if i==3 {
                
                parttimer.invalidate();
            }
            
            let btn1:CustomButton = self.testArray[i-1] as! CustomButton;
            
            if (self.sp1Btn?.isSelected)!
            {
            
                let rotationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z");
                
                rotationAnimation.values = [NSNumber(value: 0.0),NSNumber(value: M_PI*2),NSNumber(value: 0)];
                
                rotationAnimation.duration = 0.5;
                
                rotationAnimation.keyTimes = [NSNumber(value: 0),NSNumber(value: 4),NSNumber(value: 5)];
                
                
                let position = CAKeyframeAnimation(keyPath: "position")
                
                position.duration = 0.5;
                
                let mutablePath = CGMutablePath();
                
                mutablePath.move(to: btn1.startPoint!)
                
                mutablePath.addLine(to: btn1.nearPoint!)
                
                mutablePath.addLine(to: btn1.endPoint!);
                
                position.path = mutablePath;
                
                
                
                let animationGroup = CAAnimationGroup()
                
                animationGroup.animations = [position,rotationAnimation];
                
                animationGroup.duration = 0.5;
                
                //            animationGroup.isRemovedOnCompletion = false;
                
                animationGroup.fillMode = kCAFillModeForwards;
                
                animationGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                
                btn1.layer.add(animationGroup, forKey: nil);
                
                btn1.center = btn1.endPoint!;

                
            }else{
            
            
                let rotationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z");
                
                rotationAnimation.values = [NSNumber(value: 0.0),NSNumber(value: M_PI*2),NSNumber(value: 0)];
                
                rotationAnimation.duration = 0.5;
                
                rotationAnimation.keyTimes = [NSNumber(value: 0),NSNumber(value: 4),NSNumber(value: 5)];
                
                
                let position = CAKeyframeAnimation(keyPath: "position")
                
                position.duration = 0.5;
                
                let mutablePath = CGMutablePath();
                
                mutablePath.move(to: btn1.endPoint!)
                
                mutablePath.addLine(to: btn1.nearPoint!)
                
                mutablePath.addLine(to: btn1.startPoint!);
                
                position.path = mutablePath;
                
                
                
                let animationGroup = CAAnimationGroup()
                
                animationGroup.animations = [position,rotationAnimation];
                
                animationGroup.duration = 0.5;
                
                //            animationGroup.isRemovedOnCompletion = false;
                
                animationGroup.fillMode = kCAFillModeForwards;
                
                animationGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                
                btn1.layer.add(animationGroup, forKey: nil);
                
                btn1.center = btn1.startPoint!;

            }

            
            
                
        }
        
        RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)
        
        timer.fire()
        
        
        
        
        
        
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 0.2;
        
        
        if (self.sp1Btn?.isSelected)!
        {
          animation.fromValue = 0;
        
          animation.toValue = -45;
        
        } else
        {
            animation.fromValue = -45;
            
            animation.toValue = 0;
        }
        
        
        animation.isRemovedOnCompletion = false;
        
        animation.fillMode = kCAFillModeForwards;
        
        
        self.sp1Btn?.layer.add(animation, forKey: nil);
        
    }
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值