我需要爲我的應用程序進行模式鎖定。我遵循的示例是從這裏:http://blog.grio.com/2011/11/android-pattern-lock-on-iphone.htmliOS中的模式鎖定
但它是一個全屏鎖。我需要模式鎖只出現在屏幕的一部分,因爲我的屏幕也需要有文本字段和按鈕。我已經改變了一些座標,並將圖案縮小到其尺寸的四分之一。
示例中給出的代碼使用presentModalViewController:animated:
,這意味着該視圖應該全屏顯示。應該怎麼做才能使視圖不會全屏顯示?此外,當我在代碼移植到故事板一個新的項目,我得到異常NSInvalidArgument:
無法識別的選擇發送到實例
DrawPattern:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawrect...");
if (!_trackPointValue)
return;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.5, 0.5, 0.5, 0.8};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGPoint from;
UIView *lastDot;
for (UIView *dotView in _dotViews) {
from = dotView.center;
NSLog(@"drwaing dotview: %@", dotView);
NSLog(@"\tdrawing from: %f, %f", from.x, from.y);
if (!lastDot)
CGContextMoveToPoint(context, from.x, from.y);
else
CGContextAddLineToPoint(context, from.x, from.y);
lastDot = dotView;
}
CGPoint pt = [_trackPointValue CGPointValue];
NSLog(@"\t to: pt = %f, pt = %f", pt.x, pt.y);
CGContextAddLineToPoint(context, pt.x, pt.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
_trackPointValue = nil;
}
- (void)clearDotViews {
[_dotViews removeAllObjects];
}
- (void)addDotView:(UIView *)view {
if (!_dotViews)
_dotViews = [NSMutableArray array];
[_dotViews addObject:view];
}
- (void)drawLineFromLastDotTo:(CGPoint)pt {
_trackPointValue = [NSValue valueWithCGPoint:pt];
[self setNeedsDisplay];
}
的ViewController:
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return NO;
}
- (void)lockEntered:(NSString*)key {
NSLog(@"key: %@", key);
if (![key isEqualToString:@"020508"]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Wrong pattern!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alertView show];
}
else
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)lockClicked:(id)sender {
DrawPatternViewController *lockVC = [[DrawPatternViewController alloc] init];
[lockVC setTarget:self withAction:@selector(lockEntered:)];
[self presentModalViewController:lockVC animated:YES];
}
請幫幫忙! !
我刪除了初始視圖控制器,只需在DrawPatternViewController的viewDidLoad方法中的按鈕單擊方法中鍵入該部分即可。我還從DrawLockPatterView(UIView類)中提取了這些方法,負責繪製3x3網格並將其粘貼到DrawPatternViewController。這使我能夠將文本字段添加到與模式鎖定相同的視圖。但是我的上司告訴我這是不可接受的。我正試圖按照你的方法。但我不知道如何繼續。 – 2013-05-09 14:49:01
我應該使用addChildViewController嗎? – 2013-05-09 14:59:16
你的上司告訴你,這是不可接受的,但沒有提供指導?如果您使用drawRect,您仍然需要一個UIView子類:並且該子類應該像文本字段一樣處理 - 作爲子視圖添加並在控制器中配置。使用子視圖控制器可以讓鎖定管理獨立於它正在使用的地方(因此更好地重用)。 – Wain 2013-05-09 15:10:26