如何使用简单的UIVIew提供UIPopOver功能

时间:2011-04-11 14:17:39

标签: iphone objective-c uiview uislider

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

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

感谢。

2 个答案:

答案 0 :(得分:1)

在这里找一个演示,使用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   
                 }];

 }

答案 1 :(得分:-1)

使用UIAnimation将视图设置为您想要的位置。检测视图外的触摸以解除它。