保存并加载 UIScrollView 位置

发布于 2024-12-17 06:58:08 字数 2567 浏览 2 评论 0原文

我正在创建一个应用程序,它使用滚动图像的 UIScrollView 和加载新视图并允许新图像层的分段控件(有点像 Photoshop 分层)-出于某种原因,当我单击回图层时,它会记住位置但创建了一个新图层......我怎样才能阻止这种情况发生,所以一旦选择了图层并选择了图像,当用户返回时它就会转到最后加载的位置?

另外,作为一个侧面......加载 UIScrollView 时我无法使用手势识别或 UIAlertView ?有谁知道为什么?

这是我的代码...提前致谢:)

- layerControl:(NSInteger)index
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 15;
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 release];

switch (index)
{
    case 0: scroll = self.scroll1;
    case 1: scroll = self.scroll2;
    case 2: scroll = self.scroll3;
    case 4: scroll = self.scroll4;
    case 5: scroll = self.scroll5;

}

/* if (scroll == nil)
{
    [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scroll.pagingEnabled = YES;
    NSInteger viewCount = 15;
    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]; */


} 
}



//Method to add a filter to the page
- (void)addFilter:(id)sender
{
    UISegmentedControl *filterController = (UISegmentedControl *)sender;

[self layerControl:[filterController selectedSegmentIndex]];
}

 - (UIImage*)saveImages
{

UIImage *image = [UIImage imageNamed:@"UrbanPhoto##1"];

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

}  

Im creating an app that uses a UIScrollView that scrolls through images and a segmented control that loads a new view and allows a new layer of images (bit like Photoshop layering) - For some reason when I click back on the layer, it remembers the position but creates a new layer.... How can I stop this from happening, so once a layer has been selected and an image chosen, when the user returns it goes to the last loaded position?

Also, as a side thing... I cannot use my gesture recognition or UIAlertView when the UIScrollView is loaded? Does anyone know why?

Here is my code.... And thanks in advance:)

- layerControl:(NSInteger)index
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 15;
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 release];

switch (index)
{
    case 0: scroll = self.scroll1;
    case 1: scroll = self.scroll2;
    case 2: scroll = self.scroll3;
    case 4: scroll = self.scroll4;
    case 5: scroll = self.scroll5;

}

/* if (scroll == nil)
{
    [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scroll.pagingEnabled = YES;
    NSInteger viewCount = 15;
    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]; */


} 
}



//Method to add a filter to the page
- (void)addFilter:(id)sender
{
    UISegmentedControl *filterController = (UISegmentedControl *)sender;

[self layerControl:[filterController selectedSegmentIndex]];
}

 - (UIImage*)saveImages
{

UIImage *image = [UIImage imageNamed:@"UrbanPhoto##1"];

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡離愁欲言轉身 2024-12-24 06:58:08

尝试在用户操作之前保存 UIScrollView.contentOffset 的值,然后在操作完成后恢复该值。

Try saving the value of UIScrollView.contentOffset before the user action, and then restoring that value after the action is completed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文