简体   繁体   English

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

[英]Xcode image link ImageView into next View Controller

I have made a Xcode project for a tabbed application that displays images of color swatches in a Scrollview. 我已经为选项卡式应用程序制作了一个Xcode项目,该项目在Scrollview中显示色样的图像。 How do I link One of the images in my scrollview to go to the next View Controller? 如何链接滚动视图中的图像之一以转到下一个View Controller? Below is my code and pictures. 下面是我的代码和图片。 So when you click on one of the images or swatch color in the scrollview it links to the New Controller. 因此,当您在滚动视图中单击图像或色板颜色之一时,它将链接到“新控制器”。

I have multiple images that scroll down the page of the iPhone, Do I have to loop the images cause there are 24 images. 我有多个图像向下滚动到iPhone的页面,我是否必须循环播放图像,因为有24张图像。 I was able to make one button and link it to the next scene with the interface builder, But I can fit 5 images on the screen.. 我能够制作一个按钮,并使用界面生成器将其链接到下一场景,但是我可以在屏幕上容纳5张图像。

DecorsViewController_iPhone.h DecorsViewController_iPhone.h

 #import <UIKit/UIKit.h>

 @interface DecorsViewController_iPhone : UIViewController

 {
IBOutlet UIScrollView *scrollViewDecors;
 }

@property (nonatomic, retain) UIView *scrollViewDecors;

@end

DecorsViewController_iPhone.m 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

在此处输入图片说明

在此处输入图片说明

Firstly, you need to add a gesture recogniser to your view with something like: 首先,您需要使用以下方式在视图中添加手势识别器:

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

You then need to implement the 'flipView' method: 然后,您需要实现'flipView'方法:

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

As you can see in my case, when the method is triggered I perform a segue (see below): 如您所见,当触发该方法时,我将执行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];
    }
}

The 'number' variable is used to decide which image has been clicked, I divide the visible rects origin by the width of the screen in pixels to calculate which image has been pressed and therefore what data to send to my new view. 'number'变量用于确定单击了哪个图像,我将可见区域的原点除以屏幕的宽度(以像素为单位),以计算已按下的图像,从而计算出要发送到我的新视图的数据。

In your case, you would use the Y coordinate to perform a calculation to decide which colour has been pressed, possibly something like: 在您的情况下,您将使用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>;
            }

Alternatively, you could use a UITableView and just populate the cells with the appropriate colour and then display the new view based on which cell was pressed. 或者,您可以使用UITableView并仅用适当的颜色填充单元格,然后根据所按的单元格显示新视图。

EDIT Use a UITableView, and populate each cell with your image using custom tableview cells. 编辑使用UITableView,并使用自定义tableview单元格用图像填充每个单元格。 This is a much easier approach than what you are suggesting. 这比您的建议要容易得多。

See these links: adding images to UItableView 请参阅以下链接: 将图像添加到UItableView

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

You are overcomplicating this task massively, follow the above tutorials and you'll be done in no time. 您使此任务过于复杂,请按照上述教程进行操作,您将很快完成。 If this answer helped you, please mark it as correct. 如果这个答案对您有帮助,请标记为正确。

You can customize your UIImageView. 您可以自定义UIImageView。 For example, MyImageView. 例如,MyImageView。 Set its delegate to your main ScrollView and add UITapGestureRecognizer and TapDetecting Delegate methods. 将其委托设置到您的主ScrollView并添加UITapGestureRecognizer和TapDetecting委托方法。 You can implement in the MyImageView: 您可以在MyImageView中实现:

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

then in your main ScrollView(delegate of MyImageView): 然后在您的主ScrollView(MyImageView的代理)中:

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

You can use a button instead of a UIImageView, and set the button's image to your image. 您可以使用按钮代替UIImageView,然后将按钮的图像设置为您的图像。 Your loop in viewDidLoad would be like this: 您在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
}

And then you'd need to define an action for imageButton, in this case I called it pushNextViewController: 然后您需要为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.

相关问题 如何检查imageview中是否有图像,并跳过该图像到下一个imageview - How to check if an imageview has an image in it and skip over the image to the next imageview Android在ImageView上查看图像的问题 - Android issue with view image on ImageView 在ImageView上设置onclickListener以查看放大的图像 - Setting onclickListener on ImageView to view enlarged image Android:防止在 ImageView 上没有图像时转到下一个活动 - Android: Prevent go to next activity when there's no image at ImageView 应用程序不会在ImageView上随机化下一幅图像,标签Array不会与Imageview同步。 我该如何解决? - App will not randomize the next image on ImageView, tag Array is not synced with the Imageview. How do I fix this? 网格视图图像链接到活动 - Grid view image link to activites 子采样比例图像视图 - 使图像视图上的引脚标记可单击 - Subsampling scale image view - make pin markers clickable on Imageview 在另一个imageview和textview中设置列表视图项目的图像和文本 - Set image and text of list view item in another imageview and textview 回收器视图中的Imageview在每个位置加载相同的图像 - Imageview in Recycler view loads the same image in every position 将ImageView传递到下一个活动 - Pass ImageView to next activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM