繁体   English   中英

Xcode图像将ImageView链接到下一个View Controller

[英]Xcode image link ImageView into next View Controller

我已经为选项卡式应用程序制作了一个Xcode项目,该项目在Scrollview中显示色样的图像。 如何链接滚动视图中的图像之一以转到下一个View Controller? 下面是我的代码和图片。 因此,当您在滚动视图中单击图像或色板颜色之一时,它将链接到“新控制器”。

我有多个图像向下滚动到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

在此处输入图片说明

在此处输入图片说明

首先,您需要使用以下方式在视图中添加手势识别器:

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

如您所见,当触发该方法时,我将执行segue(请参见下文):

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

'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单元格用图像填充每个单元格。 这比您的建议要容易得多。

请参阅以下链接: 将图像添加到UItableView

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

您使此任务过于复杂,请按照上述教程进行操作,您将很快完成。 如果这个答案对您有帮助,请标记为正确。

您可以自定义UIImageView。 例如,MyImageView。 将其委托设置到您的主ScrollView并添加UITapGestureRecognizer和TapDetecting委托方法。 您可以在MyImageView中实现:

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

然后在您的主ScrollView(MyImageView的代理)中:

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

您可以使用按钮代替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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM