2012-09-12 50 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个图像我可以将一个图像制作成一个按钮并链接到我的下一个场景没有问题,我坚持说在我的滚动视图中放置多个按钮? –

相关问题