在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
复制下面的代码粘贴到新建的Swift工程的ViewController.swift 中就ok // // ViewController.swift // Gobing_Swift // // Created by dongqiangfei on 16/3/17. // Copyright © 2016年 dongqiangfei. All rights reserved. //
import UIKit
class ViewController: UIViewController {
var blackOrWhite : Bool!//用于交替下子 override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.lightGrayColor() blackOrWhite = true makeQiPan()//做五子棋的棋盘 // Do any additional setup after loading the view, typically from a nib. }
//做棋盘 private func makeQiPan() {
var lineNumHor : Int?//水平线的个数 lineNumHor = (Int(UIScreen.mainScreen().bounds.size.height) - 64 )/30 + 1
//树脂的 var lineNumShow : Int //竖直线的个数 lineNumShow = (Int(UIScreen.mainScreen().bounds.size.width - 15 )/30 ) + 1
for var i = 0 ; i < lineNumHor ; i++ {//水平线 let lineHoral : UIView! lineHoral = UIView.init() lineHoral.frame = CGRectMake(15, 64 + CGFloat(Float(i*30)), CGFloat(Float(lineNumShow*30)) - 30 , 1) lineHoral.backgroundColor = UIColor.blackColor() self.view .addSubview(lineHoral) }
for var i = 0; i < lineNumShow; i++ {//竖直线 let lineShow : UIView! lineShow = UIView.init() lineShow.frame = CGRectMake(15 + CGFloat(Float(i*30)), 64, 1, CGFloat(Float(lineNumHor!*30)) - 30) lineShow.backgroundColor = UIColor.blackColor() self.view .addSubview(lineShow) }
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {// 触摸事件的触发 //获得触摸的位置 var touch : UITouch! touch = touches.first var touchPoint : CGPoint! touchPoint = touch.locationInView(self.view) print("\(touchPoint.x)")
if touchPoint.y < 60 { return; }
var myView : UIView! myView = UIView.init()
//规整触摸的位置在棋盘的交叉点 var xPoint : Int! var yPoint : Int! xPoint = (Int(touchPoint.x) - 15 )%30 if xPoint > 15 { xPoint = ((Int(touchPoint.x) - 15)/30)*30 + 15 + 30 - 10 }else{ xPoint = ((Int(touchPoint.x) - 15)/30)*30 + 15 - 10 } yPoint = (Int(touchPoint.y) - 64 )%30 if yPoint > 15 { yPoint = ((Int(touchPoint.y) - 64)/30)*30 + 64 + 30 - 10 }else{ yPoint = ((Int(touchPoint.y) - 64)/30)*30 + 64 - 10 }
myView.frame = CGRectMake(CGFloat(Float(xPoint)), CGFloat(Float(yPoint)), 20, 20) myView.layer.masksToBounds = true myView.layer.cornerRadius = 10 if (blackOrWhite != nil) {//判断是白色子还是红色的字儿 myView.backgroundColor = UIColor.blackColor() blackOrWhite = nil }else { myView.backgroundColor = UIColor.whiteColor() blackOrWhite = true } self.view.addSubview(myView)//添加这个子儿
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
|
请发表评论