Keeping track of changes in a UIView

随声附和 提交于 2019-12-12 03:23:29

问题


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 loads path data from the XML file and draws a map of the Eastern United States.

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

  3. 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)

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

    Everything works fine up to here.

  5. 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.

What would be the best way to keep track of the paths the user has touched?


回答1:


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];
    }
}


来源:https://stackoverflow.com/questions/9318126/keeping-track-of-changes-in-a-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!