蛇和*游戏,分别用oc和swift开发
蛇和*游戏,分别用oc和swift开发
心血来潮想学学swift,看了一下教程和开发文档,发现其实swift的很多类都是基于oc的,比如swift的UIButton是基于oc的UIButton。所以我想其实学过oc再学swift并不是像学新语言一样难,只是把一种方言翻译成另一种方言而已。基于这个思路我试着写了一个oc的蛇和*的游戏,然后再一句一句翻译成swift,竟然成功了,仰天大笑
先介绍一下这个简单的游戏:一个25宫格的地图,上面有若干个*和蛇,摇骰子走到*下面就能顺*爬上去,走到蛇头处,要顺着蛇滑下来,走到25格即为成功。
用到的类:UIImageView、UIButton、UILabel、CAKeyframeAnimation、NSTimer
下面这个表格是我的翻译词典:
OC |
swift |
category | extension:可以写在类外面 |
- (float) check: (int) count { } | func check: (count: Int) -> Float { } |
int n = 0 n ++ |
var n: Int = 0 n += 1 |
NSString *str = @"你好" str = [NSString stringwithformat:@"%s%d", str, n]; |
var str: String = "你好" str += String(n) |
NSArray * ary = [NSArray array] ary = @[ |
var ary = [NSValue]() ary = [ NSValue(cgPoint: CGPoint(x: 0.1, y: 0.9)), NSValue(cgPoint: CGPoint(x: 9.8, y: 9.7)) ] ary.append(NSValue(cgPoint: CGPoint(x: 0.1, y: 0.9))) |
NSDictionary *dic |
var dic = [Int: CGPoint]() 字典可以用Int类型做key dic[n] = CGPoint(x:x, y:y)添加值 没有NSMutableDictionary和NSDictionary 区分 |
UIColor *white = [UIColor whiteColor] | let white:Color = UIColor.white |
UIView |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) self.view.addSubview(view) |
UILabel |
var countLabel = UILabel(frame: CGRect(x:CellHeight, y:CGFloat(CellHeight*3+wh), width:CGFloat(ViewWidth-CellHeight*2), height:(CellHeight))) countLabel.textColor = UIColor.bluecountLabel.layer.cornerRadius = 10 countLabel.layer.borderColor = UIColor.blue.cgColor countLabel.layer.borderWidth = 1 countLabel.textAlignment = NSTextAlignment.center countLabel.text = "您已走了0步" self.view.addSubview(countLabel) |
UIButton |
var diceV = UIButton(type: UIButtonType.custom) diceV.frame = CGRect(x:CGFloat((ViewWidth-shaiziWH)/2), y:CGFloat(CellHeight*5+wh), width:shaiziWH, height:shaiziWH) diceV.setBackgroundImage(UIImage(named: "1"), for: UIControlState.normal)diceV.adjustsImageWhenDisabled = false diceV.addTarget(self, action: #selector(self.shakeDice), for: UIControlEvents.touchUpInside) self.view.addSubview(diceV) //添加的点击事件 @objc func shakeDice() { } |
[NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) { // }]; | Timer.scheduledTimer(withTimeInterval: TimeInterval(0.2), repeats: false, block: { // }) |
CAKeyframeAnimation *runAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; runAnimation.values = [NSArray arrayWithArray:ary]; runAnimation.fillMode = kCAFillModeForwards; runAnimation.removedOnCompletion = NO;// 是否在动画完成后从 Layer 层上移除 回到最开始状态 runAnimation.duration = 1.0f; runAnimation.repeatCount = 0; peopleV.layer.anchorPoint = CGPointMake(0, 0); [peopleV.layer addAnimation:runAnimation forKey:nil]; |
let runAnimation:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")//只能写position runAnimation.values = [NSValue(cgPoint: geziDic[start]!), NSValue(cgPoint: geziDic[end]!)] runAnimation.duration = 1.0 runAnimation.repeatCount = 0 runAnimation.isRemovedOnCompletion = false runAnimation.fillMode = kCAFillModeForwards peopleV.layer.anchorPoint = CGPoint(x: 0, y: 0)//默认是(0.5, 0.5) peopleV.layer.add(runAnimation, forKey: nil) |