关于iOS手势解锁随笔

作者: Wo的小名叫单纯 | 来源:发表于2016-06-20 15:21 被阅读223次

    关于写手势解锁,单纯很早就想写了,只是没有时间,今天终于有时间来写一下了
    关于手势解锁的图片,是我自己随意添加的,各位可以根据自己的喜好来添加图片

    首先先写关于viewcontroller.h里面的

    @property(nonatomic,strong)UIImageView *imageView;
    
    @property(nonatomic,assign)CGPoint lineStartPoint;
    @property(nonatomic,assign)CGPoint lineEndPoint;
    
    @property(nonatomic,strong)NSMutableArray *buttonArray;
    @property(nonatomic,strong)NSMutableArray *selectedButtons;
    
    @property(nonatomic,strong)UIImage *pointImage;
    @property(nonatomic,strong)UIImage *selectedImage;
    
    @property (nonatomic, assign) BOOL drawFlag; 
    

    其次是关于viewcontroller.m里面的
    先写viewDidLoad里面的

    self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:self.imageView];
        self.imageView.backgroundColor = [UIColor whiteColor];
       
    //这里是调用的下面私有方法
        [self createLockPoints];
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        if (touch) {
            for (UIButton *btn in self.buttonArray) {
                CGPoint touchPoint = [touch locationInView:btn];
                if ([btn pointInside:touchPoint withEvent:nil]) {
                    self.lineStartPoint = btn.center;
                    self.drawFlag = YES;
                    
                    if (!self.selectedButtons) {
                        self.selectedButtons = [NSMutableArray arrayWithCapacity:9];
                    }
                    
                    [self.selectedButtons addObject:btn];
                    [btn setImage:self.selectedImage forState:UIControlStateNormal];
                }
            }
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        if (touch && self.drawFlag) {
            self.lineEndPoint = [touch locationInView:self.imageView];
            
            for (UIButton *btn in self.buttonArray) {
                CGPoint touchPoint = [touch locationInView:btn];
                
                if ([btn pointInside:touchPoint withEvent:nil]) {
                    BOOL btnContained = NO;
                    
                    for (UIButton *selectedBtn in self.selectedButtons) {
                        if (btn == selectedBtn) {
                            btnContained = YES;
                            break;
                        }
                    }
                    
                    if (!btnContained) {
                        [self.selectedButtons addObject:btn];
                        [btn setImage:self.selectedImage forState:UIControlStateNormal];
                    }
                }
            }
            
            self.imageView.image = [self drawUnlockLine];
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self outputSelectedButtons];
        
        self.drawFlag = NO;
        self.imageView.image = nil;
        self.selectedButtons = nil;
    }
    
    #pragma mark - Create Lock Points
    
    - (void)createLockPoints
    {
        self.pointImage = [UIImage imageNamed:@"2"];
        self.selectedImage = [UIImage imageNamed:@"1"];
        
        float marginTop = 100;
        float marginLeft = 45;
        
        float y;
        for (int i = 0; i < 3; ++i) {
            y = i * 100;
            float x;
            for (int j = 0; j < 3; ++j) {
                x = j * 100;
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setImage:self.pointImage forState:UIControlStateNormal];
                [btn setImage:self.selectedImage forState:UIControlStateHighlighted];
                btn.frame = (CGRect){x+marginLeft, y+marginTop, self.pointImage.size};
                [self.imageView addSubview:btn];
                btn.userInteractionEnabled = NO;
                btn.tag = LOCK_EX + i * 3 + j;
                
                if (!self.buttonArray) {
                    self.buttonArray = [NSMutableArray arrayWithCapacity:9];
                }
                [self.buttonArray addObject:btn];
            }
        }
    }
    
    #pragma mark - Draw Line
    
    - (UIImage *)drawUnlockLine
    {
        UIImage *image = nil;
        
        UIColor *color = [UIColor yellowColor];
        CGFloat width = 5.0f;
        CGSize imageContextSize = self.imageView.frame.size;
        
        UIGraphicsBeginImageContext(imageContextSize);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetLineWidth(context, width);
        CGContextSetStrokeColorWithColor(context, [color CGColor]);
        
        CGContextMoveToPoint(context, self.lineStartPoint.x, self.lineStartPoint.y);
        for (UIButton *selectedBtn in self.selectedButtons) {
            CGPoint btnCenter = selectedBtn.center;
            CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y);
            CGContextMoveToPoint(context, btnCenter.x, btnCenter.y);
        }
        CGContextAddLineToPoint(context, self.lineEndPoint.x, self.lineEndPoint.y);
        
        CGContextStrokePath(context);
        
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return image;
    }
    
    #pragma mark -
    
    - (void)outputSelectedButtons
    {
        for (UIButton *btn in self.selectedButtons) {
            [btn setImage:self.pointImage forState:UIControlStateNormal];
            NSLog(@"Selected-button's tag : %d\n", btn.tag);
        }
    }
    
    

    到此结束,图片大家根据喜好自己添加

    相关文章

      网友评论

        本文标题:关于iOS手势解锁随笔

        本文链接:https://www.haomeiwen.com/subject/udmwdttx.html