【问题标题】:How to give UIPopOver kind of functionality with simple UIVIew如何使用简单的 UIVIew 赋予 UIPopOver 类型的功能
【发布时间】:2011-04-11 14:17:39
【问题描述】:

我想制作一个 UIView,它应该显示在滑块拇指上方,并且应该显示滑块的值。我不想使用 PopOver,因为它看起来不太好,但我拥有的艺术品。

请分享您对此的看法。我不必关心苹果的政策,因为这个应用程序不会通过苹果的代码审查。这仅用于内部调查目的。

谢谢。

【问题讨论】:

    标签: iphone objective-c uiview uislider


    【解决方案1】:

    在这里找一个demo,使用UILabel和动画(虽然类名是popover..但实际上没有使用popover--),可以看源码,很简单,希望能有所帮助。

    原文链接: UILabel over slider

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      mSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
      mSlider.minimumValue = 0;
      mSlider.maximumValue = 1000;
      mSlider.value = 0;
      [mSlider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
    
      popView = [[UILabel alloc]initWithFrame:CGRectMake(mSlider.frame.origin.x, mSlider.frame.origin.y-20, 70, 20)];
      [popView setTextAlignment:UITextAlignmentCenter];
      [popView setBackgroundColor:[UIColor clearColor]];
      [popView setAlpha:0.f];
    
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      [self.window addSubview:mSlider];
      [self.window addSubview:popView];
      // Override point for customization after application launch.
      self.window.backgroundColor = [UIColor whiteColor];
      [self.window makeKeyAndVisible];
      return YES;
    }
    
    - (void)updateValue:(id)slider
    {
      UIImageView *imageView = [mSlider.subviews objectAtIndex:2];
    
      CGRect theRect = [self.window convertRect:imageView.frame fromView:imageView.superview];
    
      [popView setFrame:CGRectMake(theRect.origin.x-22, theRect.origin.y-30, popView.frame.size.width, popView.frame.size.height)];
    
      NSInteger v = mSlider.value+0.5;
      [popView setText:[NSString stringWithFormat:@"%d",v]];
    
      [UIView animateWithDuration:0.5  
                     animations:^{  
                         [popView setAlpha:1.f];
                     }  
                     completion:^(BOOL finished){  
                         // do someting when animation finished 
                     }];
    
      [timer invalidate];
      timer = nil;
      timer = [NSTimer scheduledTimerWithTimeInterval:1 
                                             target:self 
                                           selector:@selector(disPopView) 
                                           userInfo:nil repeats:NO];
    }
    
    - (void)disPopView
    {
      [UIView animateWithDuration:0.5  
                     animations:^{  
                         [popView setAlpha:0.f];
                     }  
                     completion:^(BOOL finished){  
                         //do someting when animation finished   
                     }];
    
     }
    

    【讨论】:

    • 太棒了!谢谢米多里。 =)
    【解决方案2】:

    使用 UIAnimation 将视图动画到您想要的位置。检测您视图之外的触摸以将其关闭。

    【讨论】:

    • 能否再解释一下。我对此很陌生,从来没有玩过很多 UI。请分享一些有助于我做到这一点的教程或链接。
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多