您的位置:首页 > 其它

自定义手势按住不动可以移动另一个,取消按下的状态就不移动了(做游戏可以用吧)4-13

2016-04-13 16:45 369 查看
protocol    MoveDelegate

{

    

    func     WillMoveRight()

    func     StopMoveRight()

    

}

class BtnOperate: UIButton {

    

    var   perclicktime:NSTimeInterval=0.0

    var   beginclicktime:NSTimeInterval=0.0

    var   delegate:MoveDelegate?

    override init(frame: CGRect) {

        

        

        

         super.init(frame:frame)

        

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    override func drawRect(rect: CGRect) {

        // Drawing code

    }

    */

    override   func  touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        

           let   date=NSDate()

           let   beginclick=date.timeIntervalSince1970

            beginclicktime=beginclick

        self.delegate?.WillMoveRight()

        

    }

    override    func   touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let   date=NSDate()

        let  overclick=date.timeIntervalSince1970

        

        perclicktime=overclick-beginclicktime

        

        

        

        print(perclicktime)

        self.delegate?.StopMoveRight()

    }

    

    

    

    

}
//使用自定义的view  控制

//

//  ViewController.swift

//  test

//

//  Created by lw on 16/4/9.

//  Copyright © 2016年 lw. All rights reserved.

//

import UIKit

class ViewController: UIViewController ,MoveDelegate{

    var   cus:BtnOperate!

    var   aquene:dispatch_queue_t!

    var    isstop=true

    @IBAction func move(sender: AnyObject) {

        

         self.cus.delegate=self

    

    

    }

    var    x:CGFloat=4

    @IBOutlet weak var mycation: UIButton!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let   gesture=UILongPressGestureRecognizer(target: self, action:"info:")

        

        mycation.addGestureRecognizer(gesture)

        

        let  defauts=NSUserDefaults.standardUserDefaults()

       defauts.setBool(true, forKey: "aaa")

        

        defauts.setBool(false, forKey: "bbb")

       if defauts.synchronize()

       {

        print("success")

        }

        cus=BtnOperate()

        cus.frame=CGRectMake(50, 50, 40, 60)

        cus.backgroundColor=UIColor.redColor()

        cus.delegate=self

        view.addSubview(cus)

        let   cus1=UIButton()

        cus1.frame=CGRectMake(100, 50, 40, 60)

        cus1.backgroundColor=UIColor.redColor()

        

        view.addSubview(cus1)

        cus.addTarget(self, action: "move", forControlEvents: .TouchUpInside)

        

        

        

        let   cuw=custom()

        cuw.frame=CGRectMake(100, 50, 190, 260)

        cuw.backgroundColor=UIColor.redColor()

        

        view.addSubview(cuw)

        

    }

   

   

    

    

    func  info(gesture:UILongPressGestureRecognizer)

    {

        let  defauts=NSUserDefaults.standardUserDefaults()

        defauts.setBool(false, forKey: "aaa")

        

      

      

        let  defaus=NSUserDefaults.standardUserDefaults()

       if  let s = defaus.objectForKey("aaa")  as? Bool

       {

         if   s

         {

            print("音乐开启的")

        }

        

        }

        

        print("asdas")

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    func  WillMoveRight() {

       print("去移动靶")

 

        aquene=dispatch_queue_create("right",nil)

       dispatch_async(aquene) { () -> Void in

              self.isstop=true

                while(self.isstop)

                 {

                self.x+=4

                NSThread.sleepForTimeInterval(0.5)

                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    if  self.x<=670{

                    self.mycation.frame.origin.x+=self.x

                    }

                })

                 }

        }

        

        

        

    }

    func  StopMoveRight() {

        print("停止动画")

    self.isstop=false

        

            

         

        

        

    }

}

可以在这上面扩展
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: