2012-09-12 48 views
1

我做了一個Xcode項目的選項卡式應用程序,顯示滾動視圖中的色板圖像。如何鏈接我的滾動視圖中的一個圖像以轉到下一個視圖控制器?以下是我的代碼和圖片。因此,當您在滾動視圖中點擊其中一幅圖像或色板顏色時,它會鏈接到新控制器。Xcode圖像鏈接ImageView到下一個視圖控制器

我有多個圖像向下滾動的iPhone頁面,我必須循環圖像,因爲有24個圖像。我是能夠使一個按鈕,並將其鏈接到與界面生成器的下一個場景,但我可以適應屏幕上的5張..

DecorsViewController_iPhone.h

#import <UIKit/UIKit.h> 

@interface DecorsViewController_iPhone : UIViewController 

{ 
IBOutlet UIScrollView *scrollViewDecors; 
} 

@property (nonatomic, retain) UIView *scrollViewDecors; 

@end 

DecorsViewController_iPhone.m

#import "DecorsViewController_iPhone.h" 

@interface DecorsViewController_iPhone() 

@end 

@implementation DecorsViewController_iPhone 


@synthesize scrollViewDecors; 


const CGFloat kScrollObjHeight = 81.5; 
const CGFloat kScrollObjWidth = 320.0; 
const NSUInteger kNumImages  = 24; 


- (void)layoutScrollImages 
{ 
UIImageView *view = nil; 
NSArray *subviews = [scrollViewDecors subviews]; 

// reposition all image subviews in a horizontal serial fashion 
CGFloat curXLoc = 0; 
CGFloat curYLoc = 0; 
CGFloat curYSpace = 1; 

for (view in subviews) 
{ 
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
    { 
     CGRect frame = view.frame; 
     frame.origin = CGPointMake(curXLoc, curYLoc); 
     view.frame = frame; 

     curYLoc += (curYSpace + kScrollObjHeight); 
    } 
} 

// set the content size so it can be scrollable 
    [scrollViewDecors setContentSize:CGSizeMake(([scrollViewDecors bounds].size.width), (kNumImages * kScrollObjHeight))]; // Vertical Option 
} 

- (void)viewDidLoad 
{ 
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

// 1. setup the scrollview for multiple images and add it to the view controller 
// 
// note: the following can be done in Interface Builder, but we show this in code for clarity 
[scrollViewDecors setBackgroundColor:[UIColor blackColor]]; 
[scrollViewDecors setCanCancelContentTouches:NO]; 
scrollViewDecors.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
scrollViewDecors.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
scrollViewDecors.scrollEnabled = YES; 

// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
// if you want free-flowing scroll, don't set this property. 
// scrollView1.pagingEnabled = YES; 

// load all the images from our bundle and add them to the scroll view 
NSUInteger i; 
for (i = 1; i <= kNumImages; i++) 
{ 


    NSString *imageName = [NSString stringWithFormat:@"Artwork_iPhone_Decors_Scrollview_%d.png", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect rect = imageView.frame; 
    rect.size.height = kScrollObjHeight; 
    rect.size.width = kScrollObjWidth; 
    imageView.frame = rect; 
    imageView.tag = i; // tag our images for later use when we place them in serial fashion 
    [scrollViewDecors addSubview:imageView]; 
    //[imageView release]; 
} 

[self layoutScrollImages]; // now place the photos in serial layout within the scrollview 

} 

//- (void)dealloc 
//{ 
// [scrollViewDecors release]; 
// 
// [super dealloc]; 
//} 

//- (void)viewDidLoad 
//{ 
// [super viewDidLoad]; 
// // Do any additional setup after loading the view, typically from a nib. 
//} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
} 
} 

@end 

enter image description here

enter image description here

回答

1

首先,你需要一個手勢識別器添加到您的視圖的東西,如:

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] 
                  initWithTarget:self 
                  action:@selector(flipView:)]; 
    [singleTapGestureRecognizer setNumberOfTapsRequired:1]; 
    [self.view addGestureRecognizer:singleTapGestureRecognizer]; 

然後,您需要實現 'flipView' 的方法:

- (void)flipView:(UIPanGestureRecognizer *)recognizer { 
    [self performSegueWithIdentifier:@"textView" sender:self]; 
} 

正如你可以在我的情況看,該方法時觸發我進行賽格瑞(見下文):

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"textView"]) 
    { 
     //---Pass text to view 
     CGRect visibleRect; 
     visibleRect.origin = scrollView.contentOffset; 
     visibleRect.size = scrollView.bounds.size; 

     int number; 

     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
      number = visibleRect.origin.x/768; 
     } 
     else { 
      number = visibleRect.origin.x/320; 
     } 

     TextViewController *textViewController = segue.destinationViewController; 
     AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     textViewController.text = [appDelegate.textArray objectAtIndex:number]; 
    } 
} 

的「數字」變量用來決定已被點擊該圖片,我把可見矩形以屏幕寬度爲單位,以像素爲單位計算哪個圖像已被按下,因此數據發送到我的新視圖。

在你的情況,你可以使用Y座標來進行計算,以決定已經按下的顏色,可能是這樣的:

int number; 

      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
       number = visibleRect.origin.y/<height of each image on iPad in pixels>; 
      } 
      else { 
       number = visibleRect.origin.y/<height of each image on iPhone in pixels>; 
      } 

或者,你可以使用一個UITableView,只是填充細胞適當的顏色,然後顯示基於哪個單元格被按下的新視圖。

編輯 使用UITableView,並使用自定義tableview單元格與您的圖像填充每個單元格。這比你建議的要容易得多。

請參見以下鏈接: adding images to UItableView

如果使用的是iOS 5 - http://kurrytran.blogspot.co.uk/2011/10/ios-5-storyboard-uitableview-tutorial.html 如果不是 - http://www.iosdevnotes.com/2011/10/uitableview-tutorial/

您大量過於複雜這項任務,按照上面的教程,你會在做無時間。如果此答案對您有幫助,請將其標爲正確。

+0

我怎麼會讓一個透明的按鈕在我的每個圖像上轉到一個新的視圖控制器,我有24個滾動的圖像。我可以使用界面生成器製作一個按鈕,並將它與翻轉模式鏈接起來,它可以工作,但是我只能對5張圖像做屏幕高度,我有24張圖像,這就是爲什麼我需要它滾動的原因。我是Xcode的新手。 –

+0

我不明白你爲什麼不只是使用UITableView?對於你想要達到的目標來說,這看起來很完美?只需使用cellForRowAtIndexPath填充數組,然後使用didSelectRowAtIndexPath來確定選擇了哪種顏色/單元格,從而確定顯示哪個視圖。 –

+0

我是Xcode的新手,我不確定如何填充tableview以反映我自己的自定義圖像,以及如何將錶鏈接到新視圖。我能夠輕鬆地製作標籤頁,但是在使用滾動視圖時會卡住。我擁有的圖像數量的原因。 –

0

您可以自定義您的UIImageView。例如,MyImageView。將其代表設置爲主滾動視圖,並添加UITapGestureRecognizer和TapDetecting委託方法。然後

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer { 
// single tap 
if ([_delegate respondsToSelector:@selector(myImageViewWasSingleTapped:)]) 
    [_delegate myImageViewWasSingleTapped:self]; 
} 

在你的主滾動型(MyImageView的代表): 您可以在MyImageView實施

- (void)myImageViewWasSingleTapped:(MyImageView *)miv { 
    AnotherViewController *asv = [[AnotherViewController alloc] initWithImage: miv]; 
    [self.navigationController pushViewController: asv animated:YES]; 
    [asv release]; 
} 
0

您可以使用按鈕而不是UIImageView,並將按鈕的圖像設置爲您的圖像。您在viewDidLoad中環會是這樣的:

// load all the images from our bundle and add them to the scroll view 
NSUInteger i; 
for (i = 1; i <= kNumImages; i++) 
{ 
    NSString *imageName = [NSString stringWithFormat:@"Artwork_iPhone_Decors_Scrollview_%d.png", i]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIButton *imageButton = [[UIButton alloc] init]; 
    [imageButton setImage:image forState:UIControlStateNormal]; 
    [imageButton addTarget:self action:@selector(pushNextViewController:) forControlEvents:UIControlEventTouchUpInside]; 

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
    CGRect rect = CGRectMake(0, 0, kScrollObjWidth, kScrollObjHeight); 
    imageButton.frame = rect; 
    imageButton.tag = i; // tag our images for later use when we place them in serial fashion 
    [scrollViewDecors addSubview:imageButton]; 
    // Release imageButton unless you're using ARC 
} 

然後你需要定義的ImageButton的動作,在這種情況下,我把它叫做pushNextViewController:

- (void)pushNextViewController:(id)sender 
{ 
    UIViewController *yourNextViewController = // initialise your next view controller here 
    [self.navigationController pushViewController:yourNextViewController animated:YES]; 
} 
+0

我用上面的代碼更新了我的代碼,它只在我的滾動視圖中顯示一個圖像,並且我得到了你給我的代碼的第二部分的錯誤。 (初始化UIViewController _strong與不兼容類型的表達式「void」 –

+0

我走了最遠的是能夠使圖像滾動像上面的圖片一樣,因爲有22個圖像我可以將一個圖像製作成一個按鈕並鏈接到我的下一個場景沒有問題,我堅持說在我的滾動視圖中放置多個按鈕? –

相關問題