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); }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...