繁体   English   中英

尝试使用UIScrollView显示图像

[英]Trying to display images using UIScrollView

我想创建一个简单的应用程序,该应用程序将在第一个开始屏幕上包含一个居中图像,而不是在滑动手势(向右,向左)时更改图像。

我对此很陌生,因此这里就是我正在寻找的http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/

这是我在控制器实现中的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

        [myDictionary setObject:@"img1.jpg" forKey:@"http://www.testweb.com"];
        [myDictionary setObject:@"img2.jpg" forKey:@"http://www.test2.com"];

        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        scroll.pagingEnabled = YES;
        NSInteger numberOfViews = [myDictionary count];

        for (NSString* key in myDictionary) {

                UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:[myDictionary objectForKey:key]]];
                CGRect rect = CGRectMake(10.0f, 90.0f, image.size.width, image.size.height);

                UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];

                [imageView setImage:image];

                CGPoint superCenter = CGPointMake([self.view bounds].size.width / 2.0, [self.view bounds].size.height / 2.0);

                [imageView setCenter:superCenter];
                self.view.backgroundColor = [UIColor whiteColor];

                [scroll addSubview:imageView];


        }
        scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);


        [self.view addSubview:scroll];

}

我的第一个问题是,我在初始屏幕上看到了img2而不是img1 第二个问题是,当我向右滑动时,出现白色屏幕,并且没有图像。 有什么建议我错过了什么,我可以尝试/阅读什么?

编辑:

我正在尝试使用“最轻巧”的方法,不使用任何花哨的画廊api等。只显示几个非常小的图像(即200x200 px),它们可以在屏幕上居中显示,我可以来回滑动(不应该太硬)。 好了,学习一门新语言时,一切都很难。

github MWPhotoBrowser上有一个项目,可让您放入一组图像,一次可以查看一次。 然后,您可以使用滑动手势从一个图像滚动到下一个图像。 添加功能也很容易,它应该使您对如何完成此操作有很好的了解。 还有Apple的PhotoScroller示例 ,它为您提供了如何直接执行相同操作以及平铺图像的代码。 由于您是iOS的新手,您可能希望先看看这两个示例,或者可能只是将它们用作照片查看器。

您的问题很可能是因为您正在设置CGRect rect = CGRectMake(10.0f, 90.0f, image.size.width, image.size.height); 您的两个图像视图。 这意味着您的两个 UIImageView对象都放置在完全相同的位置(两个都位于scrollview的x坐标上的10.f处)。 在最后添加第二个图像视图时,它会覆盖第一个图像,这意味着它的右边有空白。

为了解决这个问题,您可以尝试类似...

- (void)viewDidLoad
{
    //your setup code

    float xPosition = 10.f;
    for (NSString* key in myDictionary) {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:[myDictionary objectForKey:key]]];
        CGRect rect = CGRectMake(xPosition, 90.0f, image.size.width, image.size.height);
        xPosition += image.size.width;
        //rest of your code...
    }
    //rest of your code
}

上面的意思是第二个视图位于x坐标上的10 +第一个图像的宽度。 请注意,我尚未测试答案。

首先,图像彼此叠加。

CGPoint superCenter = CGPointMake([self.view bounds].size.width / 2.0, [self.view bounds].size.height / 2.0);
[imageView setCenter:superCenter];

在这里,您正在将两个图像都设置在屏幕中央。 第二件事是您使用NSDictionary并遍历密钥。 NSDictionary不是像数组那样的命令。 您将必须按特定顺序对我们的钥匙进行排序。 因此,Barjavel做得对,但是跳过了将图像设置为居中的事实。 尝试这个:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *myArray = [[NSArray alloc] initWithObjects:@"img1.jpg", @:image2.jpg", nil];
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = [myArray count];
    int xPosition = 0;
    for (NSString* image in myArray) {
        UIImage * image = [UIImage imageNamed:image];
        CGRect rect = CGRectMake(xPosition, 90.0f, image.size.width, image.size.height);

        UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
        [imageView setImage:image];
        self.view.backgroundColor = [UIColor whiteColor];
        [scroll addSubview:imageView];
        xPosition += image.size.width;
    }
    scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

    [self.view addSubview:scroll];

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM