2012-10-07 49 views
0

如何使用.nib文件中的图像添加按钮来放大和缩小按钮?我可以缩放图像,但模拟器上的运行时间中没有显示这些按钮。缩放按钮

我用下面的代码变焦:

i do what u say in the view did load and here is the code 
    - (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.visiblePopTipViews = [NSMutableArray array]; 

    self.contents = [NSDictionary dictionaryWithObjectsAndKeys: 
        // Rounded rect buttons 
        @"Play Ground", [NSNumber numberWithInt:1], 
        @"HeadMaster", [NSNumber numberWithInt:2], 
        @"Science Faculty", [NSNumber numberWithInt:3], 
        @"Accounting Faculty", [NSNumber numberWithInt:4], 
        @"Economy Faculty", [NSNumber numberWithInt:5], 
        @"Cultive Faculty", [NSNumber numberWithInt:6], 
        @"Tourisim Faculty", [NSNumber numberWithInt:7], 
        @"Swimming Pool ", [NSNumber numberWithInt:8], 
        @"Medicine Faculty", [NSNumber numberWithInt:9], 
        @"Engeneering Faculty", [NSNumber numberWithInt:10], 
        @"IT Faculty", [NSNumber numberWithInt:11], 
        nil]; 

    // Array of (backgroundColor, textColor) pairs. 
    // NSNull for either means leave as default. 
    // A color scheme will be picked randomly per CMPopTipView. 
    self.colorSchemes = [NSArray arrayWithObjects: 
         [NSArray arrayWithObjects:[NSNull null], [NSNull null], nil], 
         [NSArray arrayWithObjects:[UIColor lightGrayColor], [NSNull null], nil], 
         [NSArray arrayWithObjects:[UIColor lightGrayColor], [NSNull null], nil], 
         [NSArray arrayWithObjects:[UIColor lightGrayColor], [UIColor darkTextColor], nil], 
         [NSArray arrayWithObjects:[UIColor lightGrayColor], [UIColor blueColor], nil], 
         [NSArray arrayWithObjects:[UIColor lightGrayColor], [NSNull null], nil], 
         nil]; 
    scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    scroll.backgroundColor = [UIColor blackColor]; 
    scroll.delegate = self; 

    zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scroll.contentSize.width, scroll.contentSize.height)]; 
    zoomView.backgroundColor = [UIColor whiteColor]; 

    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.png"]]; 
    scroll.contentSize = image.frame.size; 


    UIButton *playGround = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [playGround addTarget:self 
        action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 
    [playGround setTitle:@"playGround" forState:UIControlStateNormal]; 
    playGround.tag = 1 ; 
    playGround.frame = CGRectMake(290.0, 170.0, 80.0, 40.0); 
    [image addSubview:playGround]; 

    UIButton *masterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [masterBtn addTarget:self 
        action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 
    [masterBtn setTitle:@"Presidency" forState:UIControlStateNormal]; 
    masterBtn.tag = 2 ; 
    masterBtn.frame = CGRectMake(250.0, 270.0, 60.0, 30.0); 
    [image addSubview:masterBtn]; 

    UIButton *scienceBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [scienceBtn addTarget:self 
        action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 
    [scienceBtn setTitle:@"science" forState:UIControlStateNormal]; 
    scienceBtn.tag = 3; 
    scienceBtn.frame = CGRectMake(300.0, 330.0, 50.0, 30.0); 
    [image addSubview:scienceBtn]; 

    UIButton *accountingBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [accountingBtn addTarget:self 
         action:@selector(buttonAction:) 
      forControlEvents:UIControlEventTouchDown]; 
    [accountingBtn setTitle:@"Accounting" forState:UIControlStateNormal]; 
    accountingBtn.tag = 4; 
    accountingBtn.frame = CGRectMake(340.0, 360.0, 50.0, 30.0); 
    [image addSubview:accountingBtn]; 

    UIButton *economyBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [economyBtn addTarget:self 
        action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 
    [economyBtn setTitle:@"Economy" forState:UIControlStateNormal]; 
    economyBtn.tag = 5 ; 
    economyBtn.frame = CGRectMake(390.0, 390.0, 50.0, 30.0); 
    [image addSubview:economyBtn]; 

    UIButton *cultiveBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [cultiveBtn addTarget:self 
        action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 
    [cultiveBtn setTitle:@"Cultive" forState:UIControlStateNormal]; 
    cultiveBtn.tag = 6 ; 
    cultiveBtn.frame = CGRectMake(440.0, 410.0, 50.0, 30.0); 
    [image addSubview:cultiveBtn]; 

    UIButton *tourisimBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [tourisimBtn addTarget:self 
        action:@selector(buttonAction:) 
      forControlEvents:UIControlEventTouchDown]; 
    [tourisimBtn setTitle:@"tourisim" forState:UIControlStateNormal]; 
    tourisimBtn.tag = 7 ; 
    tourisimBtn.frame = CGRectMake(480.0, 440.0, 50.0, 30.0); 
    [image addSubview:tourisimBtn]; 

    UIButton *swimmingBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [swimmingBtn addTarget:self 
        action:@selector(buttonAction:) 
      forControlEvents:UIControlEventTouchDown]; 
    [swimmingBtn setTitle:@"swimming" forState:UIControlStateNormal]; 
    swimmingBtn.tag = 8 ; 
    swimmingBtn.frame = CGRectMake(190.0, 400.0, 50.0, 30.0); 
    [image addSubview:swimmingBtn]; 

    UIButton *medicineBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [medicineBtn addTarget:self 
        action:@selector(buttonAction:) 
      forControlEvents:UIControlEventTouchDown]; 
    [medicineBtn setTitle:@"medicine" forState:UIControlStateNormal]; 
    medicineBtn.tag = 9; 
    medicineBtn.frame = CGRectMake(250.0, 420.0, 70.0, 30.0); 
    [image addSubview:medicineBtn]; 

    UIButton *enegeneeringBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [enegeneeringBtn addTarget:self 
         action:@selector(buttonAction:) 
       forControlEvents:UIControlEventTouchDown]; 
    [enegeneeringBtn setTitle:@"engeneering" forState:UIControlStateNormal]; 
    enegeneeringBtn.tag = 10 ; 
    enegeneeringBtn.frame = CGRectMake(340.0, 460.0, 60.0, 30.0); 
    [image addSubview:enegeneeringBtn]; 

    UIButton *itBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [itBtn addTarget:self 
       action:@selector(buttonAction:) 
    forControlEvents:UIControlEventTouchDown]; 
    [itBtn setTitle:@"It" forState:UIControlStateNormal]; 
    itBtn.tag = 11 ; 
    itBtn.frame = CGRectMake(410.0, 500.0, 60.0, 30.0); 
    [image addSubview:itBtn]; 

    [zoomView addSubview:image ] ; 

    [scroll addSubview:zoomView]; 
    [scroll bringSubviewToFront:zoomView ] ; 
    // [self.view addSubview:scroll ] ; 

    scroll.minimumZoomScale =scroll.frame.size.width/image.frame.size.width; 
    scroll.maximumZoomScale = 2.0; 
    [scroll setZoomScale:scroll.minimumZoomScale]; 
    self.view = scroll; 
    [scroll release]; 
} 


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
    return image ; 
} 

回答

0

此的loadView方法是在你的视图控制器,是不是? 当View Controller需要加载其视图时调用此方法。如果将按钮添加到笔尖文件中,并且正在调用此方法,那么这些按钮将不会显示 - 它正在加载您的代码,而不是您的笔尖。

你应该做的是将UIScrollView作为子视图添加,或者在nib文件中设置更多。 如果你确实要做它的代码,一定不要覆盖self.view,但添加子视图。在viewDidLoad中执行此操作,而不是loadView。

这样做,你的按钮应该显示出来。

+0

我使它像这样,但我不能点击按钮 – user1709830

+0

您是否将UIButtons添加到UIImageView ...?那很有意思。你并没有完全写出我想象中的那样,但它应该做... 你应该看看如何使用loadView,viewDidLoad以及如何从一个笔尖加载。这将使写这些东西变得轻而易举。 如果你只是想让它工作,我认为你不能得到按钮的原因是因为它们是_inside_一个UIImageView,它默认情况下具有userInteractionEnabled = NO,所以没有任何子视图(在这种情况下,按钮)可以与之交互。设置image.userInteractionEnabled = YES,看看会发生什么。 – cjwirth

+0

它最终与我合作,正确的代码,,我必须在滚动[滚动addSubView:图像]上添加图像,但在我定义按钮之前,我删除了我在这种情况下提示thnx 4的视图,我们必须不断尝试在代码中知道究竟是什么工作 – user1709830