1
A
回答
1
我想你可以使用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
附加手勢識別的觀點:
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)
}
相關問題
- 1. iOS:UIScrollView檢測刷卡手勢
- 2. 使用手勢控制
- 3. 用手勢控制視頻
- 4. Android手勢刷卡「意外停止」
- 5. UITableView的滑動手勢衝突刷卡
- 6. UIImageView上的手勢識別器(刷卡)
- 7. UIButton和滑動手勢
- 8. 在UIButton上調用的默認手勢?
- 9. 使用C#處理手勢和使用Selenium和Appium刷卡
- 10. 如何用觸摸手勢控制CAKeyframeAnimation?
- 11. 偵聽刷卡手勢不起作用的代碼
- 12. 我可以禁用IE10歷史刷卡手勢嗎?
- 13. 翻譯觸控手勢鼠標手勢
- 14. 使用自定義導航控制器時啓用回手勢滑動手勢
- 15. 關閉::創建一個用刷卡和輕擊手勢的UIButton水平旋轉木馬
- 16. UIButton上的長按手勢識別器?
- 17. 手勢識別器的UIButton子視圖
- 18. UIButton受到手勢的影響
- 19. UIButton檢測按住「手勢識別器」?
- 20. 個人形象手勢控制
- 21. 的iOS手勢碰撞對控制器
- 22. 持續的手勢/控制觸摸
- 23. 滑動手勢和導航控制器
- 24. 刷卡用手機刪除
- 25. 刷卡手勢不能在xcode中工作
- 26. ImageSwitcher與手勢檢測器「一扔」/「刷卡」
- 27. 滑動佈局之間沒有刷卡或手勢在Android
- 28. 如何實現UIImageView隨機工具的刷卡手勢
- 29. dojox /手勢/刷卡防止HTML選擇打開下拉
- 30. MMDrawerController:檢測菜單關閉操作或刷卡手勢
能否請你指導我如何填寫看法? –
看到我的編輯謝謝! – Haroon
謝謝@哈羅。但我需要進一步的解釋,在UIView子類或UIVewController中使用UIPanGestureRecognizer。以及如何將FingerPositionInView傳遞給子類? –