如何将具有UIGesture的UIImage添加到应用程序中的大多数视图. [英] How to add a UIImage with UIGesture to most views in an application.

查看:44
本文介绍了如何将具有UIGesture的UIImage添加到应用程序中的大多数视图.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中制作广告横幅.有点像iAd.

I want to make an Advertising banner in my app. A bit like iAd's.

我要通过在视图上放置UIImage然后分配横幅图像来实现此目的.然后,我将添加一个触摸手势,以便用户可以单击它并转到我的应用程序中的另一个视图.我知道我可以很容易地在一个视图上执行此操作,但是我希望在应用程序的大多数视图上都可以执行此操作.将横幅添加到一个以上视图而不用一次编写相同的代码的最佳方法是什么?

I was going to make it by having a UIImage on the view then assigning the banner image. I would then add a touch gesture so the user could click it and go to another view in my app. I Know That I can do this on one view quite easily but I want this to be on most views in the app. Whats the best way for adding the banner to more than one view with out writing the same code more that once?

下面的设计显示了横幅广告的种类.

The below design shows the sort of banner im after.

谢谢

推荐答案

您可以创建一个UIView类,并将其命名为BannerView.//在bannerView.h

You can Create a UIView Class and call it BannerView for instance. // in the bannerView.h

    #import <UIKit/UIKit.h>
@interface BannerView : UIView{ 
    UIImageView* bannerImage;
}
@property(nonatomic,retain) UIImageView* bannerImage;    
@end

//在bannerView.m

//in the bannerView.m

#import "BannerView.h"

@implementation BannerView
@synthesize bannerImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    bannerImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"banner-image.png"]];
    bannerImage.frame=CGRectMake(0, 0, 320, 100);
    [self addSubview:bannerImage];

// add a uibutton on top of the uiimageview and assign an action for it 
// better than creating an action recogniser

    UIButton* actionButton=[UIButton buttonWithType:UIButtonTypeCustom];
    actionButton.frame=CGRectMake(0, 0, 320, 100);
    [actionButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:actionButton];
}

-(void) yourAction{
// do what ever here like going to an other uiviewController as you mentionned 

}

@end

现在您可以通过这种方式从任何View Controller调用此视图

Now you can call this view from any View Controller this way

 BannerView* banner=[[BannerView alloc] initWithFrame:CGRectMake(0, 300, 320, 100)];
    [self.view addSubview:banner];

这篇关于如何将具有UIGesture的UIImage添加到应用程序中的大多数视图.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆