如何水平设置popover位置

     2023-03-12     158

关键词:

【中文标题】如何水平设置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 视图以正确关闭

】如何设置Popover视图以正确关闭【英文标题】:HowtosetupPopoverviewstodismissproperly【发布时间】:2011-02-0819:16:06【问题描述】:经过一些工作,我得到了一个弹出视图,其中显示了一个自定义uiview,其中显示正确。当您在弹出视图... 查看详情

Swift 3 Popover 昏暗背景

...】:2017-07-1903:47:45【问题描述】:我已经阅读了多个关于如何完成此操作的建议。我在后台添加了一个UI视图并将其设置为禁用,然后在显示弹出框后,将视图设置为启用。如您所见,它看起来运行良好:但我确实有两个问题。... 查看详情

水平滚动时 DataTables Fixed Column 上的 Popover 看起来很奇怪

】水平滚动时DataTablesFixedColumn上的Popover看起来很奇怪【英文标题】:PopoveronDataTablesFixedColumnlooksstrangewhenhorizontallyscrolling【发布时间】:2020-06-2400:31:06【问题描述】:我有一个问题,我试图将弹出框放在已修复的表的第一列上。... 查看详情

如何修改tableau里图例的位置

一、将图例Legend放于图表右侧1、设置chart的marginRight属性值chart:marginRight:120,2、设置legend图例属性值如下legend:align:'right',//水平方向位置verticalAlign:'top',//垂直方向位置x:0,//距离x轴的距离y:100//距离Y轴的距离,二、将图例... 查看详情

如何关闭 AirPrint Popover?

】如何关闭AirPrintPopover?【英文标题】:HowdoIdismisstheAirPrintPopover?【发布时间】:2011-06-2203:08:11【问题描述】:我有一个单例弹出框,所以我一次只显示一个弹出框。当我执行共享弹出框并选择AirPrint时,共享弹出框正确消失,... 查看详情

IOS AdaptiveView Popover 和 NavigationController:它们是如何工作的?

】IOSAdaptiveViewPopover和NavigationController:它们是如何工作的?【英文标题】:IOSAdaptiveViewPopoverandNavigationController:howdotheywork?【发布时间】:2015-08-1408:31:50【问题描述】:我正在将一个旧应用程序更新为新的自适应大小的处理方式,... 查看详情

如何使用 Xcode Storyboards 创建 Popovers

】如何使用XcodeStoryboards创建Popovers【英文标题】:HowtocreatePopoverswithXcodeStoryboards【发布时间】:2012-01-0503:39:01【问题描述】:如何在iPadStoryboard中为iOS5.0+应用创建弹出框?Xcode项目模板(实用程序)已经有一个设置,但我似乎无... 查看详情

无法让 Popover 在对话框中的正确位置显示

】无法让Popover在对话框中的正确位置显示【英文标题】:Can\'tgetPopovertodisplayincorrectpositioninDialog【发布时间】:2020-02-1722:46:40【问题描述】:我有一个对话框和一个ListItem,当您单击它时,它会显示一个弹出框进入编辑模式。这... 查看详情

设置滚动条的位置(代码片段)

链接:http://www.w3school.com.cn/jquery/css_scrollleft.asp设置水平滚动条位置设置所有匹配元素的水平滚动条位置。$(selector).scrollLeft(position)设置竖直滚动条位置设置所有匹配元素的水平滚动条位置。$(selector).scrollTop(position)   查看详情

如何仅显示从当前位置到水平 RecyclerView 的一项?

】如何仅显示从当前位置到水平RecyclerView的一项?【英文标题】:HowcanIshowonlyoneitemfromcurrentpositiontohorizontalRecyclerView?【发布时间】:2020-09-1707:40:42【问题描述】:我有具有GridView的水平RecyclerView。当我滚动RecyclerView时,同时出现2... 查看详情

如何使 div 以固定位置水平居中? [复制]

】如何使div以固定位置水平居中?[复制]【英文标题】:howtomakedivcenterhorizontallywithfixedposition?[duplicate]【发布时间】:2013-04-1003:26:45【问题描述】:我想让div水平居中,css代码是这样的:<styletype="text/css">#footerposition:fixed;bottom... 查看详情

如何同步容器内动态水平滚动视图的滚动视图位置?

】如何同步容器内动态水平滚动视图的滚动视图位置?【英文标题】:HowdoIsynchronizescrollviewpositionsofdynamichorizontalscrollviewsinsideacontainer?【发布时间】:2020-03-0922:00:02【问题描述】:我的一个活动中有一个父容器,它调用它的子容... 查看详情

css里如何让div水平平均分布以至于放大缩小界面都不会改变位置

就是把divflot-left以后他不就是x轴排列了吗我想让他基于屏幕或者是基于他的父级宽度水平分布用什么命令在放大缩小后也不会乱跑不是很明白你的意思,是不是比如把界面分为4列的意思?如果是,设置float:left;width:25%;这样就可... 查看详情

如何使用 CABasicAnimation 水平移动球并将球停在正确的位置

】如何使用CABasicAnimation水平移动球并将球停在正确的位置【英文标题】:HowtomoveaballhorizontallywithCABasicAnimationandstoptheballatthecorrectplace【发布时间】:2010-11-2317:15:03【问题描述】:我正在尝试编写游戏,但我对这个动画有点卡住了... 查看详情

背景图片位置设置为百分比

...以是百分数,大部分值都很好理解,但是50%50%这两个值是如何计算的呢?图片水平和垂直居中。与background-position:centercenter;效果等同。等同于x:{容器(container)的宽度 查看详情

如何在 XCode 11 中更改图像的位置以进行纵向或水平对齐

】如何在XCode11中更改图像的位置以进行纵向或水平对齐【英文标题】:HowtochangepositionofimageforportraitorhorizontalalignmentinXCode11【发布时间】:2020-07-3002:20:59【问题描述】:我试图在水平方向查看时从填充屏幕右侧的图像到纵向查看... 查看详情

React -- Material UI -- Popover, setAnchorEl(null) onClose 的 popover 出于某种原因没有将其设置为 null

】React--MaterialUI--Popover,setAnchorEl(null)onClose的popover出于某种原因没有将其设置为null【英文标题】:React--MaterialUI--Popover,setAnchorEl(null)onCloseofthepopoverdoesnotsetittonullforsomereason【发布时间】:2021-02-0518:25:56【问题描述】:我在MenuItem 查看详情

css如何使背景图片水平居中

CSS中定位背景图片的属性是:background-position,用法background-position属性设置背景图像的起始位置。你要水平居中可以:div{background-position:center center}//第一个center是水平居中,第二个center是上下居中CSS(层叠样式表)级联样式... 查看详情