跟踪UIView中的更改 [英] Keeping track of changes in a UIView

查看:67
本文介绍了跟踪UIView中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子类UIView(currentMapView),它使用从XML文件提取的一系列CGMutablePaths绘制了多个状态的地图。当用户点击状态时,填充颜色将从绿色变为紫色。现在,我可以使用CGPathContainsPoint找出正在动态点击的状态,以报告返回了哪个路径,然后设置该路径的填充颜色并使用setNeedsDisplay重新加载视图。我的问题是我需要重复使用currentMapView几次(不同的状态组),而且我不确定应该如何跟踪用户使用的路径,以便在重新加载时可以重新着色

I have a subclassed UIView (currentMapView) that draws a map of several states using a series of CGMutablePaths pulled from an XML file. When the user taps on a state the fill color changes from green to purple. Right now I figure out which state was tapped on the fly using CGPathContainsPoint to report back which path was tapped and I then set the fill color of that path and reload the view using setNeedsDisplay. The problem I have is that I need to re-use currentMapView several times (different groups of states) and I'm not sure how I should go about tracking which paths the user has tapped on so I can color them again when I reload the view from the XML file.

这是我正在做的分步示例:

Here's a step by step example of what I'm doing:


  1. currentMapView从XML文件加载路径数据并绘制美国东部的地图。

  1. currentMapView loads path data from the XML file and draws a map of the Eastern United States.

用户点按佛罗里达州,然后变成紫色(使用CGContextSetFillColorWithColor设置佛罗里达路径的填充颜色后,我在currentMapView上调用 setNeedsDisplay。

User taps Florida, and it turns purple (I call 'setNeedsDisplay' on currentMapView after setting the fill color of the Florida path using CGContextSetFillColorWithColor)

用户点击北卡罗莱纳州,它变成紫色(在使用CGContextSetFillColorWithColor设置北卡罗莱纳州路径的填充颜色后,在currentMapView上称为 setNeedsDisplay)

User taps North Carolina, and it turns purple (I call 'setNeedsDisplay' on currentMapView after setting the fill color of the North Carolina path using CGContextSetFillColorWithColor)

用户导航到另一个视图,我需要在currentMapView中绘制另一组状态(清除其中的状态)。

User navigates to another view and I need to draw another group of states in currentMapView (wiping out what it there).

在这里一切正常。

当用户从第一步返回视图时,我绘制了美国东部的地图再次我需要在佛罗里达和北卡罗莱纳州的道路上着色。

When the user returns to the view from step one and I draw map of the Eastern United States again I need to color in the Florida and North Carolina paths again.

什么是最好的跟踪方式

推荐答案

最简单的方法是拥有全局 NSMutableSet 名为 statesTouched 的变量。将其初始化为 application:didFinishLaunchingWithOptions:中的空集。

The easiest way would be to have a global NSMutableSet variable named statesTouched. Initialize it to an empty set in application:didFinishLaunchingWithOptions:.

每次用户点击状态时,都添加集合的状态名称(或对表示状态的对象的引用):

Each time the user taps a state, add the name of the state (or a reference to an object that represents the state) to the set:

[statesTouched addObject:touchedStateName];

使用状态加载视图时,请在视图中循环浏览状态,并检查每个状态是否在集合:

When you load the view with states, loop over the states in the view and check whether each is in the set:

for (NSString *stateName in viewStateNames) {
    if ([statesTouched member:stateName]) {
        [self setColor:[UIColor purpleColor] forStateName:stateName];
    }
}

这篇关于跟踪UIView中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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