我做了一個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
我怎麼會讓一個透明的按鈕在我的每個圖像上轉到一個新的視圖控制器,我有24個滾動的圖像。我可以使用界面生成器製作一個按鈕,並將它與翻轉模式鏈接起來,它可以工作,但是我只能對5張圖像做屏幕高度,我有24張圖像,這就是爲什麼我需要它滾動的原因。我是Xcode的新手。 –
我不明白你爲什麼不只是使用UITableView?對於你想要達到的目標來說,這看起來很完美?只需使用cellForRowAtIndexPath填充數組,然後使用didSelectRowAtIndexPath來確定選擇了哪種顏色/單元格,從而確定顯示哪個視圖。 –
我是Xcode的新手,我不確定如何填充tableview以反映我自己的自定義圖像,以及如何將錶鏈接到新視圖。我能夠輕鬆地製作標籤頁,但是在使用滾動視圖時會卡住。我擁有的圖像數量的原因。 –