Swift实现弹出菜单

前端之家收集整理的这篇文章主要介绍了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,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: 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,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); } 原文链接:https://www.f2er.com/swift/321837.html

猜你在找的Swift相关文章