1

我需要为我的应用程序制作一个模式锁。我下面的例子来自这里:http: //blog.grio.com/2011/11/android-pattern-lock-on-iphone.html

但是,它是全屏锁定。我需要图案锁定仅出现在屏幕的一部分中,因为我的屏幕也需要有文本字段和按钮。我更改了其中的一些坐标,并将图案缩小到其大小的四分之一。

示例中给出的代码使用presentModalViewController:animated:这意味着视图应全屏显示。应该怎么做才能使视图不全屏显示?此外,当我将代码移植到带有情节提要的新项目时,我将异常NSInvalidArgument:无法识别的选择器发送到实例

绘制图案:

- (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];
}

视图控制器:

- (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];
}

请帮忙!!

4

1 回答 1

0

您需要将“锁定视图”添加为带有其他文本字段的视图的子视图,并将控制代码迁移到控制该视图的视图控制器。这样你就没有需要推送的视图控制器了。

从技术上讲,您可以改为将 'lock vies 的视图控制器添加为子视图控制器(更好地供将来重用)。在这种情况下,“锁定视图”仍被添加为带有其他文本字段的视图的子视图。

于 2013-05-09T06:54:50.163 回答