Snap to grid effect with Quartz 2d? iphone dev(使用 Quartz 2d 对齐网格效果?iphone开发)
问题描述
仍然是 iphone 开发入门者,但正在尝试.
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];//告诉图像自己绘制
CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - >verticalOffset); [drawImage drawAtPoint:drawPoint]; //Tell the image to draw itself
我在 Viewcontroller 上有一个网格作为背景,与该网格重叠我有一个带有自定义绘图的 UIView,每次用户触摸屏幕时都会显示一个图钉.
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?
推荐答案
假设你的做法是让用户点击,然后将 pin 放在最近的网格交叉点,这基本上只是一个舍入问题.
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];
使用 nearbyint() 进行舍入,事情可能会更整洁一些:
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开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Quartz 2d 对齐网格效果?iphone开发
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
