接着之前的 五子棋项目写的
棋盘已经画好了 下面开始添加棋子
Simulator Screen Shot - iPhone XS Max - 2019-05-09 at 22.26.41.png计算棋子的位子
- 画一个棋盘
// 画一个标准的棋盘
- (UIImage *)drawImageCheckerboard
{
CGSize size = CGSizeMake(SCREEN_WIDTHL - 30, SCREEN_WIDTHL - 30);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 0.8f);
/*每个格子的宽度*/
CGFloat gridWidth = size.width/gridCount;
self.gridWidth = gridWidth;
/**
画一个外框
*/
CGPoint aPoints[4];//坐标点
aPoints[0] =CGPointMake(0, 0);//坐标1
aPoints[1] =CGPointMake(size.width, 0);//坐标2
aPoints[2] =CGPointMake(size.width, size.height);
aPoints[3] =CGPointMake(0, size.height);
CGContextAddLines(ctx, aPoints, 5);
for (int i = 0; i <= gridCount - 1; i ++) {
CGContextMoveToPoint(ctx, i * gridWidth + gridWidth , gridWidth);
CGContextAddLineToPoint(ctx, i * gridWidth + gridWidth , (gridCount - 1) * gridWidth);
}
for (int i = 0 ; i <= gridCount - 1; i ++) {
CGContextMoveToPoint(ctx, gridWidth , gridWidth * i + gridWidth);
CGContextAddLineToPoint(ctx, gridWidth * (gridCount - 1) , i * gridWidth + gridWidth);
}
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextStrokePath(ctx);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- 添加点击事件的方法
self.imageView = [[UIImageView alloc] initWithImage:[self drawImageCheckerboard]];
[self.view addSubview:self.imageView];
self.imageView.frame = CGRectMake(15, Height_NavBar + 50, SCREEN_WIDTHL - 30, SCREEN_WIDTHL - 30);
self.imageView.backgroundColor = RGB16(0xCD9B1D);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapaction:)];
[self.imageView addGestureRecognizer:tap];
self.imageView.userInteractionEnabled = true;
- 获取点击的位置
- 获取带点击的位置, 在位置上添加 0.5 的半径, 除以半径长度 , 可以得到关于位置 , x 和 y 的坐标
- (void)tapaction: (UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:self.imageView];
NSInteger rol = (point.x - self.gridWidth * 0.5)/self.gridWidth;
NSInteger cow = (point.y - self.gridWidth * 0.5)/self.gridWidth;
// 将点击的位置住转换到对应点上面去
CGPoint piecPoint = CGPointMake(rol, cow);
}
- 在点击的位置上添加 棋子
#pragma mark -- 添加棋子的坐标
- (BOOL)addPieseWithPoint: (CGPoint)point
{
//转换成为对应的坐标点
CGPoint arcPoint = CGPointZero;
arcPoint.x = (point.x + 0.5) * self.gridWidth;
arcPoint.y = (point.y + 0.5) * self.gridWidth;
// 生成棋子对象
PieceOBJ *piec_obj = [[PieceOBJ alloc] init];
piec_obj.isBlack = self.manager.isBlack;
piec_obj.point = point;
if ([self.manager checkIsSuccessWithPieceObj:piec_obj] == false) {
return false;
}
UIColor *color = RGB16(0xF7F7F7);
if (self.manager.isBlack) {
color = [UIColor blackColor];
}
[self addLayerFrame:CGRectMake(arcPoint.x + 2.5, arcPoint.y + 2.5, self.gridWidth - 5, self.gridWidth - 5) color:color];
if (self.manager.isWin) {
[self someBoddyWinWithIsBlack:self.manager.isBlack];
return false;
}
self.manager.isBlack = !self.manager.isBlack;
return true;
}
// 实现添加 棋子的方式
- (void)addLayerFrame: (CGRect )frame color: (UIColor *)color
{
// 创建layer
CAShapeLayer *arcLayer = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
// 颜色的填充
arcLayer.fillColor = color.CGColor;
// 实现画圆的位置和路径
CGPathAddEllipseInRect(path, nil, frame);
arcLayer.path = path;
CGPathRelease(path);
// 完成画圆, 将棋子添加到 图片上显示出来
[self.imageView.layer addSublayer:arcLayer];
}
网友评论