自定义手势按住不动可以移动另一个,取消按下的状态就不移动了(做游戏可以用吧)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
}
}
可以在这上面扩展
{
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
}
}
可以在这上面扩展
相关文章推荐
- Map
- 程序员需要具备的基本技能
- 安卓github项目
- iOS中webview和js的交互
- Servlet和Tomcat部署
- 开机小脚本自动打开sublime text 和git-bash
- 走迷宫
- WOJ 1578 Fishing Dream(莫队算法)
- full gc频繁的分析及解决案例
- 【笔试/面试】—— 二叉树的深度和宽度
- 双重 BFS —— POJ 1475 Pushing Boxes
- spring mvc CommonsMultipartResolver文件上传maxUploadSize限制大小
- Direct2D Intro - Load Bitmap From Resource
- 百万连接之路
- Flow chart drawing
- MediaPlayer播放audio
- POJ 3061 Subsequence
- Node 编程规范
- Android Wear API 中文 ---- 翻译目录
- 设计模式-观察者模式Observer