How to give UIPopOver kind of functionality with simple UIVIew
I want to make a UIView which should get displayed above the slider thumb and should show the value of slider. I don't want to use the PopOver as it's not looking good but the artwork i have.
Please share your views on this. I don't have to care about the apple policies as this application would not go through apple's code review. This is only for 开发者_StackOverflowthe internal survey purpose.
Thanks.
Find a demo here, using UILabel and animation(though the class name is popover..but actually not using popover--), you can go through the source code, it's very simple, hope it can help.
Original link here: 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
}];
}
Use UIAnimation to animate the view to the position you want it. Detect touches outside of your view to dismiss it.
精彩评论