保存 UIScrollView 的视图
我正在构建一个应用程序,其中的一部分允许您滚动浏览不同的图像以进行叠加。我有一个分段控件来制作一种分层系统,其中每个分段都允许您再次滚动图像并构建图层。由于某种原因,第一个图像总是粘住,当我返回到图层时,视图会重置到其原始位置,但也会保存最后一个位置?
这是我的代码。
- (void)layerControl
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 8;
for (int i = 0; i < viewCount; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
//[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll autorelease];
//Determine the current filter in view of the scroll view
CGFloat pageWidth = scroll.frame.size.width;
int cFilter = floor((scroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
currentFilter = cFilter;
}
//Method to add a filter to the page
- (void)addFilter:(id)sender
{
UISegmentedControl *filterController = (UISegmentedControl *)sender;
switch ([filterController selectedSegmentIndex])
{
case 0: self.layerControl;
case 1: self.layerControl;
case 2: self.layerControl;
case 3: self.layerControl;
case 4: self.layerControl;
case 5: self.layerControl;
default: NULL;
}
}
I am building an app where part of it allows you to scroll through different images to overlay. I have a segmented control to make a sort of layering system where each segment allows you to scroll through the images again and build up layers. For some reason first image always sticks and when I return to a layer, the view is reset to its original position but also saves the last position?
Here is my code.
- (void)layerControl
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 8;
for (int i = 0; i < viewCount; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
//[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll autorelease];
//Determine the current filter in view of the scroll view
CGFloat pageWidth = scroll.frame.size.width;
int cFilter = floor((scroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
currentFilter = cFilter;
}
//Method to add a filter to the page
- (void)addFilter:(id)sender
{
UISegmentedControl *filterController = (UISegmentedControl *)sender;
switch ([filterController selectedSegmentIndex])
{
case 0: self.layerControl;
case 1: self.layerControl;
case 2: self.layerControl;
case 3: self.layerControl;
case 4: self.layerControl;
case 5: self.layerControl;
default: NULL;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来也许每次你调用 self.layerControl 时你都会在视图上创建一个新的 UIScrollView 。
所以旧的仍然存在并且处于旧的位置,但是你添加了另一个。
也许存储对每个 UIScrollView 的引用,并在 layerControl 的开头抓取给定索引的滚动视图。
所以:
然后在你的addFilter方法中调用:
而不是大的switch语句。
顺便说一句,像所有 self.layerControl 调用那样使用点语法调用方法并不是一个好主意。
当您访问合成属性时,您应该保留点语法。
It looks like maybe every time you call self.layerControl you are creating a new UIScrollView on the view.
So the old one is still there and in the old position, but you've added another one.
Perhaps store a reference to each UIScrollView and at the beginning of layerControl grab the scrollview for the given index.
So:
Then in your addFilter method call:
rather than the big switch statement.
As an aside, it's not a great idea to call methods with dot syntax like you have with all those self.layerControl calls.
You should leave dot syntax for times when you're accessing a synthesized property.