2011-12-08 7 views
0

私は自分のuislidersにポップアップビューを追加しました。私はすでにそれをしていた男からのポップアップでカスタムスライダのコードを手に入れました。ポップアップが表示されていますが、唯一の問題は、スライダーがあるセルの内側にポップアップが表示されているため、セルの最後で切り取られることです。 ポップアップビューをセルの前面に表示するにはどうすればよいですか? は、私はあきらめたので、私はそれを把握するカント[OK]をセルの前で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 

(私は複数のスライダー異なるセル内の各1を持っています)。これがスライダとそのpopupviewのコードです。誰もが全部を読むような気がするなら、私は助けを使うことができます:P

答えて

0

UITableViewのサブビューとしてpopupviewを追加しようとすることができます。

スライダーと一緒に移動するには、スライダーの位置をテーブルビューに関連させて計算する必要があります。

これは、例えば、UIViewののconvertPoint:toView:方法を使用することによって達成することができます:slider.superviewあなたのUITableViewCellだろう

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

sliderButtonPointはスライダーの丸いボタンの真ん中トップポイントになります(例えば)、 tableViewはあなたのUITableViewです。

これを少し遊んでみると、テーブルビューをスクロールするときに奇妙な振る舞いが見られることがありますが、これが最初に試してみることです。

+0

mmmだから、 "sliderButtonPoint"がどこにあるのか推測すると、ノブがある点で置き換えてください。しかし、どのようにノブポイントを取得する必要がありますか? – lascort

+0

私は実際にあなたのカスタムスライダがポップアップでどのように動作するのか分からないので、スライダのつまみがあなたのポイントであるかどうかは分かりません。単にポップアップが表示されるポイントを取るか、スライダーの真ん中など – Mutix

+0

ええ、そのノブは、それは愚かな質問だった、私はまだそれを理解しようとしているが、それは難しい、あなたの答えのためにありがとう – lascort

関連する問題