0

この質問は重複していると思いますが、見つかりません =(. (iPhone/iPad)*.xib からロードされる独自の UIView クラスを作成する方法

私は次のことを試していました:

@interface CurtainView : UIView

...

- (id)init {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
         self = [[[NSBundle mainBundle] loadNibNamed:@"CurtainView_iphone" owner:self options:nil] objectAtIndex:0];
        [self setFrame:CGRectMake(0, 0, 320, 460)];
    }
    else {                
        self = [[[NSBundle mainBundle] loadNibNamed:@"CurtainView_ipad" owner:self options:nil] objectAtIndex:0];
        [self setFrame:CGRectMake(0, 0, 768, 1004)];
    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    NSLog(@"there should be some animation on view appirance");

}

と ...

CurtainView* curtain = [[CurtainView alloc] init];
NSLog(@"before");
[self.view addSubview:curtain];
[curtain drawRect:CGRectMake(0, 0, 320, 460)];

しかし、この場合、期待した結果が得られず、drawRect が呼び出されません。ユニバーサル アプリ用のカスタム ビューを簡単に作成できる方法があることを願っています。

4

2 に答える 2

0

このコードを .h ファイルに入れます #import

@interface CurtainView : UIView

@end

このコードを .m ファイルに入れます

#import "CurtainView.h"

@implementation CurtainView

- (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
   NSLog(@"there should be some animation on view appirance");
}

このUIViewフォームをviewcontrollerクラスと呼びます。これは drawRect を呼び出します

CurtainView* curtain = [[CurtainView alloc] init];
NSLog(@"before");
[curtain drawRect:self.view.frame];
[self.view addSubview:curtain];

ビューコントローラークラスを呼び出すことができます

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
 } else {
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
 }
于 2012-04-05T13:43:13.317 に答える