将PopUpView放在单元格前面

时间:2011-12-08 13:47:49

标签: ios popup cell uislider

所以,我给我的uisliders添加了一个popupview。我得到了自定义滑块的代码,其中包含一个已经完成该操作的人的弹出窗口。弹出窗口正在显示,但唯一的问题是弹出窗口显示在滑块所在的单元格内,因此它会在单元格的末尾被切断。 如何将弹出视图放在单元格前? (我在不同的单元格中有多个滑块)

#import "MNEValueTrackingSlider.h"
#import "ToothTableViewController.h"
#pragma mark - Private UIView subclass rendering the popup showing slider value
@interface MNESliderValuePopupView : UIView {
MNEValueTrackingSlider *trackingSlider;
ToothTableViewController *toothViewController;
}
@property (nonatomic) float value;
@property (nonatomic, retain) UIFont *font;
@property (nonatomic, retain) NSString *text;
@end

#import "MNEValueTrackingSlider.h"
#import "ToothTableViewController.h"
@implementation MNESliderValuePopupView

@synthesize value=_value;
@synthesize font=_font;
@synthesize text = _text;

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
    self.font = [UIFont boldSystemFontOfSize:18];
}
return self;
}

- (void)dealloc {
self.text = nil;
self.font = nil;
[super dealloc];
}

- (void)drawRect:(CGRect)rect {

// Set the fill color
[[UIColor colorWithWhite:0 alpha:0.8] setFill];

// Create the path for the rounded rectanble
CGRect roundedRect = CGRectMake(self.bounds.origin.x , self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height * 0.8);
UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:6.0];

// Create the arrow path
UIBezierPath *arrowPath = [UIBezierPath bezierPath];
CGFloat midX = CGRectGetMidX(self.bounds);
CGPoint p0 = CGPointMake(midX, CGRectGetMaxY(self.bounds));
[arrowPath moveToPoint:p0];
[arrowPath addLineToPoint:CGPointMake((midX - 10.0), CGRectGetMaxY(roundedRect))];
[arrowPath addLineToPoint:CGPointMake((midX + 10.0), CGRectGetMaxY(roundedRect))];
[arrowPath closePath];

// Attach the arrow path to the buble
[roundedRectPath appendPath:arrowPath];

[roundedRectPath fill];

// Draw the text
if (self.text) {
    [[UIColor colorWithWhite:1 alpha:0.8] set];
    CGSize s = [_text sizeWithFont:self.font];
    CGFloat yOffset = (roundedRect.size.height - s.height) / 2;
    CGRect textRect = CGRectMake(roundedRect.origin.x, yOffset, roundedRect.size.width, s.height);

    [_text drawInRect:textRect 
             withFont:self.font 
        lineBreakMode:UILineBreakModeWordWrap 
            alignment:UITextAlignmentCenter];    
}
}

- (void)setValue:(float)aValue {
_value = aValue;
self.text = [NSString stringWithFormat:@"%4.2f", _value];
[self setNeedsDisplay];
}

@end

#pragma mark - MNEValueTrackingSlider implementations
#import "ToothTableViewController.h"
@implementation MNEValueTrackingSlider

@synthesize thumbRect;
@synthesize sliderButtonPoint;
#pragma mark - Private methods

- (void)_constructSlider {
valuePopupView = [[MNESliderValuePopupView alloc] initWithFrame:CGRectZero];
valuePopupView.backgroundColor = [UIColor clearColor];
valuePopupView.alpha = 0.0;
toothViewController = [[ToothTableViewController alloc] init];
[self addSubview:valuePopupView];
}

- (void)_fadePopupViewInAndOut:(BOOL)aFadeIn {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (aFadeIn) {
    valuePopupView.alpha = 1.0;
} else {
    valuePopupView.alpha = 0.0;
}
[UIView commitAnimations];
}

- (void)_positionAndUpdatePopupView {
CGRect _thumbRect = self.thumbRect;
CGRect popupRect = CGRectOffset(_thumbRect, 0, -(_thumbRect.size.height * 1.5));
valuePopupView.frame = CGRectInset(popupRect, -20, -10);
valuePopupView.value = (NSInteger)self.value;
}

#pragma mark - Memory management

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
    [self _constructSlider];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
    [self _constructSlider];
}
return self;
}

- (void)dealloc {
[valuePopupView release];
[super dealloc];
}

#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. Only in this case show the popup-view
if(CGRectContainsPoint(self.thumbRect, 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 popoup view
[self _fadePopupViewInAndOut:NO];
[super endTrackingWithTouch:touch withEvent:event];
}

#pragma mark - Custom property accessors

- (CGRect)thumbRect {
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbR = [self thumbRectForBounds:self.bounds 
                                     trackRect:trackRect
                                         value:self.value];
return thumbR;
}

@end

好的,所以我放弃了,我无法弄明白。这是滑块及其弹出视图的代码。如果有人想读完整件事,我可以使用帮助:P

1 个答案:

答案 0 :(得分:0)

您可以尝试将popupview添加为UITableView的子视图。

然后要将它与滑块一起移动,您必须通过获取滑块相对于您的tableview的位置来计算该点。

这可以通过使用UIView的convertPoint:toView:方法来实现,例如:

CGPoint sliderButtonRelativePoint = [slider.superview convertPoint:sliderButtonPoint toView:tableView];

其中slider.superview将是您的UITableViewCell,sliderButtonPoint将是滑块圆形按钮的中间顶点(例如),tableView将是,嗯...你的UITableView的。

你可能不得不稍微玩一下这个,你可能会发现在滚动tableview时有一些奇怪的行为,但这就是我先尝试的。