用Quartz 2d对齐网格效果? iPhone开发 [英] Snap to grid effect with Quartz 2d? iphone dev

查看:152
本文介绍了用Quartz 2d对齐网格效果? iPhone开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想让用户触摸屏幕,在这个屏幕上点(pin)将被放置在视图上。



我已经完成了这个(yey),但是我想抓取效果,并且不能提出解决方案。
这就是我如何放置那个点:$ b​​


CGPoint drawPoint = CGPointMake(lastTouch.x - horizo​​ntalOffset,lastTouch.y - > verticalOffset);
[drawImage drawAtPoint:drawPoint]; //告诉图像绘制自己


我有一个网格作为Viewcontroller的背景,与那个网格重叠,我有一个UIView每次用户触摸屏幕时显示一个别针的自定义绘图。



(出于某种原因,点在用户触及之前已经出现......尽管这是我目前正在研究的另一个查询)



因此,捕捉到网格效应。任何想法?假设你的方法是让用户点击,然后把引脚放在最近的网格交点处,这基本上只是一个舍入问题。

  //假设这些常量
// kVGridOffset,kHGridOffset:视图的原点
//和网格的原点
// kVGridSpacing,kHGridSpacing:网格本身的大小

//设置触摸点的初始坐标
CGPoint drawPoint = CGPointMake(lastTouch.x - horizo​​ntalOffset,lastTouch.y - verticalOffset);

//删除偏移量,舍入到最近的间距增量,并返回偏移量
drawPoint.x = floor((drawPoint.x - kHGridOffset)/ kHGridSpacing + 0.5)* kHGridSpacing + kHGridOffset ;
drawPoint.y = floor((drawPoint.y - kVGridOffset)/ kVGridSpacing + 0.5)* kVGridSpacing + kVGridOffset;

//绘制图像
[drawImage drawAtPoint:drawPoint];

编辑 nearbyint )对于四舍五入来说,事情可以有点整洁:

  drawPoint.x = nearbyint((drawPoint .x  -  kHGridOffset)/ kHGridSpacing)* kHGridSpacing + kHGridOffset; 
drawPoint.y = nearbyint((drawPoint.y - kVGridOffset)/ kVGridSpacing)* kVGridSpacing + kVGridOffset;


Still an iphone dev starter but trying.

I would like to have the user touch the screen at which point a pin (dot) will be placed on the view.

I have accomplished this (yey) but i would like to have snap to grip effect, and can not come up with a solution. This is how i am placing that dot:

CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - >verticalOffset); [drawImage drawAtPoint:drawPoint]; //Tell the image to draw itself

I have a grid as a background on a Viewcontroller, overlapping that grid i have a UIView with custom drawing that displays a pin every time the user touches the screen.

(and for some reason the dot already appears before the user touches... all though that is another query that i am looking into at the moment)

So snap to grid effect. Any ideas?

解决方案

Assuming that your approach is to let the user tap, and then put the pin at the closest grid intersection, this is basically just a rounding problem.

// assume these constants
// kVGridOffset, kHGridOffset: the difference between the origin of the view
//   and the origin of the grid
// kVGridSpacing, kHGridSpacing: the size of the grid itself

// set initial coordinates from touch
CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset);

// remove the offset, round to nearest increment of spacing, and return offset
drawPoint.x = floor((drawPoint.x - kHGridOffset) / kHGridSpacing + 0.5) * kHGridSpacing + kHGridOffset;
drawPoint.y = floor((drawPoint.y - kVGridOffset) / kVGridSpacing + 0.5) * kVGridSpacing + kVGridOffset;

// draw the image
[drawImage drawAtPoint:drawPoint];

EDIT: with the availability of nearbyint() for rounding, things can be a little neater:

drawPoint.x = nearbyint((drawPoint.x - kHGridOffset) / kHGridSpacing) * kHGridSpacing + kHGridOffset;
drawPoint.y = nearbyint((drawPoint.y - kVGridOffset) / kVGridSpacing) * kVGridSpacing + kVGridOffset;

这篇关于用Quartz 2d对齐网格效果? iPhone开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆