iOS中的模式锁定

时间:2013-05-09 05:45:14

标签: ios objective-c xcode4.5

我需要为我的应用程序进行模式锁定。我所关注的例子来自:http://blog.grio.com/2011/11/android-pattern-lock-on-iphone.html

然而,它是全屏锁定。我需要模式锁定只出现在屏幕的一部分,因为我的屏幕也需要有文本字段和按钮。我已经更改了一些坐标,并将模式降低到其大小的四分之一。

示例中给出的代码使用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];
}

请帮助!!

1 个答案:

答案 0 :(得分:0)

您需要添加锁定视图'作为具有其他文本字段的视图的子视图,并将控制代码迁移到控制该视图的视图控制器。然后你就不会有一个需要推动的视图控制器。

从技术上讲,您可以将锁定视图控制器添加为子视图控制器(以便将来重用)。在这种情况下,锁定视图'仍然作为视图的子视图添加到其他文本字段。