如何在ios中单个视图中滑动多个图像

时间:2014-02-21 10:39:26

标签: ios iphone objective-c

我在单个视图中进行了多次图像滑动。在该应用程序中,只有一个视图上有图像。我希望能够从左到右和从右到左滑动,让第一张图像离开视图,第二张图像进入。我正在尝试代码。但它不适合我。请任何身体指南我。我是图片中的新手。提前谢谢。

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 190)];   
scrollView.showsVerticalScrollIndicator=YES;
scrollView.userInteractionEnabled=YES;
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollWidth = 120;    
scrollView.contentSize = CGSizeMake(scrollWidth,80);    
[self.view addSubview:scrollView];


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSError *err;
    NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"response is %@",strResponse);

    dict1=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];

    NSLog(@"vals are %@",dict1);

    NSString *strImg;
    url=@"myimageurl";
    str=[dict1 valueForKey:@"image_names"];

    arrImages=[str componentsSeparatedByString:@","];

   for(int index=0; index < [arrImages count]; index++)   
   {       
        NSLog(@"image: %@",[arrImages objectAtIndex:index]);
        if (str!=nil) 
        {  
            strImg=[url stringByAppendingString:[arrImages objectAtIndex:index]];

            dispatch_async(dispatch_get_global_queue(0,0), ^{

                NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strImg]];

               if(data == nil) return;

               dispatch_async(dispatch_get_main_queue(), ^{

                   UIImage *img=[UIImage imageWithData: data];
                   imgView.image=img;
               });
            });
        }
    }
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch * touch = [[event allTouches] anyObject];

    for(int index=0;index<[arrImages count];index++)
    {
        UIImageView *imgView = [arrImages objectAtIndex:index];

        NSLog(@"x=%f,y=%f,width =%f,height=%f",imgView.frame.origin.x,imgView.frame.origin.y,imgView.frame.size.width,imgView.frame.size.height);

        NSLog(@"x= %f,y=%f",[touch locationInView:self.view].x,[touch locationInView:self.view].y) ;

        if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollview]))
        {
            [self ShowDetailView:imgView];
            break;
        }
     }
  }

3 个答案:

答案 0 :(得分:0)

首先设置pagingEnabled属性scrollview.pagingEnabled = YES;。 然后为contentSize设置正确的scrollView。 最后但同样重要的是,您必须将width的{​​{1}}设置为contentSize的倍数。

即。如果您有3张图片且scrollView.frame.size.width的{​​{1}}为320像素,则scrollView必须为320x3 = 960像素

答案 1 :(得分:0)

您可能需要scrollView.contentSize.width 调整amountOfImages * scrollWidth


除此之外,你真的应该为每个图像创建一个新对象。

它可以处理图像下载,验证和触摸处理(即使图像上只有一个透明按钮)。你可以完全消除控制器中那些奇怪的touchesBegan代码,你可以完全忘记坐标。

此外,您可以停止以这种方式滚动的下载进度,从而提高性能。

答案 2 :(得分:0)

    .h file #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController<UIScrollViewDelegate,UIScrollViewAccessibilityDelegate>

    {
        UIScrollView  *scrView;
        NSMutableArray *arrimage;
        UIImageView *imgview;
    }
    @end
.M file
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.



    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scrollView setBackgroundColor:[UIColor redColor]];
    scrollView.contentSize = CGSizeMake(320, 465);

    [scrollView setScrollEnabled:YES];
    [scrollView setPagingEnabled:YES];
    [scrollView setAlwaysBounceVertical:NO];
    [self.view addSubview:scrollView];

    arrimage = [[NSMutableArray alloc]initWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg",@"5.jpeg",@"6.jpeg",@"7.jpeg",@"8.jpeg",@"9.jpeg",@"10.jpeg", nil];

    for (int i = 0; i < [arrimage count]; i++)
    {
        CGFloat xOrigin = i * scrollView.frame.size.width;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:[arrimage objectAtIndex:i]]];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];

        [scrollView addSubview:imageView];
    }

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrimage count], scrollView.frame.size.height)];
}
相关问题