簡體   English   中英

如何在iOS中的單個視圖中滑動多個圖像

[英]how to swipe the multiple images in single view in ios

我在單個視圖中進行了多幅圖片滑動。在該應用中,只有一個視圖上有圖像。 我希望能夠從左向右和從右向左滑動,使第一張圖像不顯示在視圖中,而第二張圖像進入其中。我正在嘗試代碼,但對我而言不起作用。請使用任何機身指南我是我的新手,感謝您。

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

首先設置pageingEnabled屬性scrollview.pagingEnabled = YES; 然后為scrollView設置正確的contentSize 最后但並非最不重要的一點是,您必須將contentSizewidth設置為scrollView.frame.size.width

IE如果您有3張圖片,並且您的scrollViewwidth為320像素,則contentSize必須為320x3 = 960像素

您可能需要調整scrollView.contentSize.widthamountOfImages * scrollWidth


除此之外,您確實應該為每個圖像創建一個新對象。

它可以處理圖像的下載,驗證和觸摸處理(即使圖像上只有一個透明按鈕)。 您可以完全清除控制器上的怪異touchesBegan代碼,而可以完全忘記坐標。

您也可以停止以這種方式滾動的下載進度,從而提高性能。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM