2014-02-10 61 views

回答

1

Download Code

我想你可以使用UIPanGestureRecognizer,但試用UIView而不是UIButton所以,你使用Core Graphics來填充(繪製)colo根據手指的移動。

要顯示的答案使用UILabel超過UIView

For UIPanGestureRecognizer Tutorial

//Color Filling 
//Add This code to UIView subClass that you will use instead of `UIButton` 
- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // The color to fill the rectangle (in this case black) 
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); 

    // draw the filled rectangle 
    CGRect fillRect = CGRectMake(0,0,FingerPositionInView,self.bounds.size.height) 
    CGContextFillRect (context, fillRect); 
} 

//終極密碼

.h File 
#import <UIKit/UIKit.h> 

@interface MHAnswerView : UIView 
{ 
    float fingerPositionInView; 
} 
- (id)initWithFrame:(CGRect)frame andAnswer:(NSString*)answer; 

@end 

.m File 

#import "MHAnswerView.h" 

@implementation MHAnswerView 

- (id)initWithFrame:(CGRect)frame andAnswer:(NSString*)answer 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self.layer setBorderColor:[UIColor colorWithRed:185.0f/255.0f green:224.0f/255.0f blue:231.0f/255.0f alpha:1.0f].CGColor]; 
     [self.layer setBorderWidth:2.0f]; 
     [self.layer setCornerRadius:2.0f]; 
     [self setBackgroundColor:[UIColor whiteColor]]; 
     UILabel* lblAnswer = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; 
     lblAnswer.textAlignment = NSTextAlignmentCenter; 
     lblAnswer.text = answer; 
     [self addSubview:lblAnswer]; 

     UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
     [self addGestureRecognizer:panGesture]; 
     [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(updateProgressView) userInfo:nil repeats:YES]; 

    } 
    return self; 
} 
-(void)updateProgressView 
{ 
     [self setNeedsDisplayInRect:self.bounds]; 
} 

// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // The color to fill the rectangle (in this case black) 
    CGContextSetRGBFillColor(context,185.0f/255.0f,224.0f/255.0f,231.0f/255.0f,1.0); 
    //CGContextSetRGBFillColor(context,0.0,0.0f,0.0f,1.0); 
    // draw the filled rectangle 
    CGRect fillRect = CGRectMake(0,0,fingerPositionInView,self.bounds.size.height); 
    CGContextFillRect (context, fillRect); 
} 
-(void)move:(UIPanGestureRecognizer*)sender 
{ 
    CGPoint translation = [sender translationInView:self]; 
    NSLog(@"%f", translation.x+self.frame.origin.x); 
    fingerPositionInView = translation.x + self.frame.origin.x; 

} 

//Adding MHView to View Controller 
MHAnswerView* mhview = [[MHAnswerView alloc] initWithFrame:CGRectMake(20, 20, 280, 100) andAnswer:@"Answer"]; 
    [self.view addSubview:mhview]; 
+0

能否請你指導我如何填寫看法? –

+0

看到我的編輯謝謝! – Haroon

+0

謝謝@哈羅。但我需要進一步的解釋,在UIView子類或UIVewController中使用UIPanGestureRecognizer。以及如何將FingerPositionInView傳遞給子類? –

0

附加手勢識別的觀點:

UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
[view addGestureRecognizer:pan]; 

在你的處理器獲取手指的x位置:

- (void)handlePan:(UIPanGestureRecognizer*)sender 
{ 
    CGPoint translation = [sender translationInView:[view superview]]; 
    NSLog(@"%f", translation.x) 
}