2014-02-21 51 views
0

我在單個視圖中製作了多個圖像拖動。在該應用中,只有一個視圖具有圖像。我希望能夠從左到右,從右到左滑動,並讓第一張圖片離開視圖,第二張圖片進入。我正在嘗試編碼。但它不適用於我。請任何身體指南我是新來的圖像swipping.Thanks提前。如何在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; 
    [email protected]"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; 
     } 
    } 
    } 
+0

任何機構幫我請 – user3222991

+0

任何機構幫助我please.i我打這個issue..still我還沒有找到一個解決方案......... – user3222991

回答

0

首先設置pagingEnabled屬性scrollview.pagingEnabled = YES;。 然後爲scrollView設置正確的contentSize。 最後但並非最不重要的是,您必須將widthcontentSize設置爲scrollView.frame.size.width的倍數。

I.E.如果你有3個圖像和您的scrollView擁有320像素的widthcontentSize必須320x3 = 960像素

+0

感謝您response.I正在增加scrollview.pagingEnabled = YES; - (void)viewDidLoad方法。但仍然不起作用... – user3222991

+0

如果你在XIB中使用InterfaceBuilder,把代碼放在'viewDidLoad'內,否則放在'loadView'中。 編輯您的答案並向我顯示您已編寫的更新代碼。 – Fry

+0

感謝您response.but如果我把代碼中的loadView越來越像exceptions.so請指導me.Thanks提前.... – user3222991

0

您可能需要調整scrollView.contentSize.width通過amountOfImages * scrollWidth


除此之外,你真的應該爲每個圖像創建一個新的對象。

它可以處理圖像下載,驗證和觸摸處理(即使在圖像上方有一個透明按鈕)。你可以完全消除你的控制器中奇怪的touchesBegan代碼,你完全可以忘記座標。

此外,您可以停止以這種方式滾動的進度下載,因此性能可能會增長。

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

請發表意見代碼或留下解釋。 – Shawn