1

我创建了基于视图的应用程序。并添加了从 UIView 扩展的新类。该类中包含一些控件(简单控件)。现在我想在用户点击某些东西时显示/隐藏这个控件。控件应显示为覆盖窗口。在所有其他控件之上。我尝试这样做:

- (void)viewDidLoad {
    [super viewDidLoad];        
    addSubview:myOverlayChannelPicker;
}

一切正常,调试进入自定义控制代码,但屏幕上没有任何显示。我尝试手动添加它并且它可以工作,但我需要在运行时添加它。不显示控件的原因可能是什么?

OverlayChannelPicker 的完整代码

@implementation OverlayChannelPicker
@synthesize myImage;
@synthesize img1;
@synthesize img2;

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

- (void)awakeFromNib{
    img1 = [UIImage imageNamed:@"imgTest.jpg"];
    img2 = [UIImage imageNamed:@"imgTest2.jpg"];

    arrayOfImages = [[NSMutableArray alloc] init];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];
    [arrayOfImages addObject:img1];
    [arrayOfImages addObject:img2];

    scrollView = [[UIScrollView alloc] init];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = NO; // for smooth scroll
    scrollView.directionalLockEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    //scrollView.backgroundColor = [UIColor blueColor];
    scrollView.autoresizesSubviews = YES;
    scrollView.frame = CGRectMake(0, 150, 320, 128);
    [scrollView setContentSize:CGSizeMake(1620, 128)];
    [self addSubview:scrollView];

    UIImage *imageToAdd;
    int x = 0;
    int y = 0;
    for(imageToAdd in arrayOfImages)
    {       
        UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];     

        temp.frame = CGRectMake(x, y, 128, 128);
        temp.userInteractionEnabled = YES;      
        x += 135;


        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
        [temp addGestureRecognizer:tap];    

        [scrollView addSubview:temp];
    }

}

- (void)imageTapped:(UIGestureRecognizer *)sender
{       
    UIImageView *iv = (UIImageView *)[sender view];
    UIImage *image = [iv image];
    UIImage *it = [arrayOfImages indexOfObject:image];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:iv cache:YES];

    if(it == img1)
        it = img2;
    else
        it = img1;

    [UIView commitAnimations];  
}

它的标题:

@interface OverlayChannelPicker : UIView <UIScrollViewDelegate>{
    IBOutlet UIImageView *myImage;
    UIImage *img1;
    UIImage *img2;  

    IBOutlet UIScrollView *scrollView;
    int view1Index;
    int view2Index; 
    NSMutableArray *arrayOfImages;

}
@property (nonatomic, retain) UIImageView *myImage;
@property (nonatomic, retain) UIImage *img1;
@property (nonatomic, retain) UIImage *img2;
@property (nonatomic, retain) UIScrollView *scrollView;

- (void)awakeFromNib;
@end
4

1 回答 1

0

您可能希望像这样拨打电话:

[self.view addSubview:myOverlayChannelPicker];

通过查看您发布的代码示例,我不得不假设您之前没有进行过任何 Objective-C 开发。如果是这种情况,您可能应该从这里开始介绍 Objective-C 编程语言以了解基础知识。

于 2010-12-01T14:34:34.837 回答