如何水平设置popover位置
Posted
技术标签:
【中文标题】如何水平设置popover位置【英文标题】:how to set popover position horizontally 【发布时间】:2014-12-04 08:49:48 【问题描述】:我正在制作一个我正在使用滑块的应用程序,并且在更改滑块的滑块值的位置时也正在滑动,现在我被卡住了,当我以水平形式更改滑块的位置时我想设置然后我还需要以水平形式更改弹出位置。我附上一个屏幕截图,以便大家更好地理解它,这是我的项目链接https://www.dropbox.com/s/n0fl34h6t8jlazn/CustomSlider.zip?dl=0,
下面是我在 1 个视图上的示例代码,我正在将我的 uislider 类更改为 anpopoverslider:
-(void)constructSlider
_popupView = [[ANPopoverView alloc] initWithFrame:CGRectZero];
_popupView.backgroundColor = [UIColor clearColor];
_popupView.alpha = 0.0;
[self addSubview:_popupView];
下面是我的 ANpopoverslider
#import "ANPopoverSlider.h"
@implementation ANPopoverSlider
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
// Initialization code
[self constructSlider];
return self;
-(id)initWithCoder:(NSCoder *)aDecoder
self = [super initWithCoder:aDecoder];
if (self)
[self constructSlider];
return self;
#pragma mark - UIControl touch event tracking
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
// Fade in and update the popup view
CGPoint touchPoint = [touch locationInView:self];
// Check if the knob is touched. If so, show the popup view
if(CGRectContainsPoint(CGRectInset(self.thumbRect, -12.0, -12.0), touchPoint))
[self positionAndUpdatePopupView];
[self fadePopupViewInAndOut:YES];
return [super beginTrackingWithTouch:touch withEvent:event];
-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
// Update the popup view as slider knob is being moved
[self positionAndUpdatePopupView];
return [super continueTrackingWithTouch:touch withEvent:event];
-(void)cancelTrackingWithEvent:(UIEvent *)event
[super cancelTrackingWithEvent:event];
-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
// Fade out the popup view
[self fadePopupViewInAndOut:NO];
[super endTrackingWithTouch:touch withEvent:event];
#pragma mark - Helper methods
-(void)constructSlider
CGRect frame = CGRectMake(150, 230, 300.0, 10.0);
_popupView = [[ANPopoverView alloc] initWithFrame:frame];
_popupView.backgroundColor = [UIColor clearColor];
_popupView.alpha = 0.0;
[self addSubview:_popupView];
-(void)fadePopupViewInAndOut:(BOOL)aFadeIn
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (aFadeIn)
_popupView.alpha = 1.0;
else
_popupView.alpha = 0.0;
[UIView commitAnimations];
-(void)positionAndUpdatePopupView
CGRect zeThumbRect = self.thumbRect;
CGRect popupRect = CGRectOffset(zeThumbRect, 0, -floor(zeThumbRect.size.height * 1.5));
_popupView.frame = CGRectInset(popupRect, -20, -10);
_popupView.value = self.value;
#pragma mark - Property accessors
-(CGRect)thumbRect
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbR = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
return thumbR;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
// Drawing code
*/
@end
这是我的popover类
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
self.font = [UIFont boldSystemFontOfSize:15.0f];
UIImageView *popoverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sliderlabel.png"]];
[self addSubview:popoverView];
textLabel = [[UILabel alloc] init];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.font = self.font;
textLabel.textColor = [UIColor colorWithWhite:1.0f alpha:0.7];
textLabel.text = self.text;
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.frame = CGRectMake(0, -2.0f, popoverView.frame.size.width, popoverView.frame.size.height);
[self addSubview:textLabel];
return self;
-(void)setValue:(float)aValue
_value = aValue;
self.text = [NSString stringWithFormat:@"%4.2f", _value];
textLabel.text = self.text;
[self setNeedsDisplay];
【问题讨论】:
【参考方案1】:我尝试了示例项目并使用了简单的功能
@property (strong, nonatomic) IBOutlet UISlider *slider; // make the getter and setter for Slider
@property (strong, nonatomic) IBOutlet UILabel *lblText; // make the getter and setter for label
- (void)viewDidLoad
[super viewDidLoad];
// set verticical of UIslider
CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
self.slider.transform = trans;
// get the events of UISlider
[self.slider addTarget:self
action:@selector(sliderDidEndSliding:)
forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
// this method used for hide the label after changed the value
- (void)sliderDidEndSliding:(NSNotification *)notification
NSLog(@"Slider did end sliding...");
[self.lblText removeFromSuperview];
// the slider value change method
- (IBAction)slider:(UISlider*)sender
// add the subview the lable to slider
[self.slider addSubview:self.lblText];
self.lblText.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sliderlabel.png"]];
self.lblText.text = [NSString stringWithFormat:@"%4.2f",sender.value];
// change the label position depend upon slider move
self.lblText.center = CGPointMake(self.slider.value*self.slider.bounds.size.width,80);
[self.lblText setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
这里我附上了UISlider
的示例项目下载link
【讨论】:
非常感谢 :)以上是关于如何水平设置popover位置的主要内容,如果未能解决你的问题,请参考以下文章
iPad开发--UIPopoverController简单使用iOS7之前和iOS7之后的使用方法