触摸事件练习 -- 手势解锁
生活随笔
收集整理的這篇文章主要介紹了
触摸事件练习 -- 手势解锁
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="vXZ-lx-hvc"><dependencies><plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/></dependencies><scenes><!--View Controller--><scene sceneID="ufC-wZ-h7g"><objects><viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController"><view key="view" contentMode="scaleToFill" id="kh9-bI-dsS"><rect key="frame" x="0.0" y="0.0" width="320" height="480"/><autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/><subviews><imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Home_refresh_bg" id="70g-Bh-a7j"><rect key="frame" x="0.0" y="0.0" width="320" height="480"/><autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/></imageView><view contentMode="scaleToFill" id="SVk-1V-3b7" customClass="LWTLockView"><rect key="frame" x="0.0" y="80" width="320" height="320"/><autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/><color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/><color key="tintColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/><connections><outlet property="delegate" destination="vXZ-lx-hvc" id="P3U-ve-9af"/></connections></view></subviews><color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/></view></viewController><placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/></objects></scene></scenes><resources><image name="Home_refresh_bg" width="640" height="1008"/></resources><simulatedMetricsContainer key="defaultSimulatedMetrics"><simulatedStatusBarMetrics key="statusBar"/><simulatedOrientationMetrics key="orientation"/><simulatedScreenMetrics key="destination"/></simulatedMetricsContainer> </document> View CodeLWTViewController.h
#import <UIKit/UIKit.h>@interface LWTViewController : UIViewController@end View CodeLWTViewController.m
#import "LWTViewController.h" #import "LWTLockView.h"@interface LWTViewController ()<LWTLockViewDelegate>@end@implementation LWTViewController- (void)lockViewDidClick:(LWTLockView *)lockView andPwd:(NSString *)pwd {NSLog(@"LWTViewController %@",pwd); }@end View CodeLWTLockView.h
#import <UIKit/UIKit.h> @class LWTLockView;@protocol LWTLockViewDelegate <NSObject>- (void)lockViewDidClick:(LWTLockView *)lockView andPwd:(NSString *)pwd;@end@interface LWTLockView : UIView@property (nonatomic, strong) IBOutlet id<LWTLockViewDelegate> delegate;@end View CodeLWTLockView.m
// // LWTLockView.m // 手勢解鎖 // // Created by apple on 14-6-12. // Copyright (c) 2014年 lwt. All rights reserved. // #import "LWTLockView.h" #define KTotalCols 3 #define KTotalBtn 9@interface LWTLockView () /*** 保存選中的所有按鈕*/ @property (nonatomic, strong) NSMutableArray *btns; /*** 定義屬性,記錄用戶當前手指的位置(非按鈕范圍內(nèi))*/ @property (nonatomic, assign) CGPoint currentPoint;@end@implementation LWTLockView- (NSMutableArray *)btns {if (!_btns) {_btns = [NSMutableArray array];}return _btns; }// 當視圖是通過代碼創(chuàng)建出來的就會調(diào)用initWithFrame - (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {// Initialization code [self setup];}return self; }// 當視圖從xib或storyboard中創(chuàng)建出來就會調(diào)用 - (id)initWithCoder:(NSCoder *)aDecoder {self = [super initWithCoder:aDecoder];if (self) {// Initialization code [self setup];}return self; }/*** 創(chuàng)建9個按鈕添加到自定view中*/ - (void) setup {for (int i = 0; i < KTotalBtn; i++) {// 創(chuàng)建按鈕UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];// 設(shè)置按鈕的背景圖片[btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];// 設(shè)置按鈕的tag作為唯一標識btn.tag = i + 1;// 添加按鈕到View [self addSubview:btn];// 禁止按鈕的點擊事件(因為我們需要監(jiān)聽觸摸事件)btn.userInteractionEnabled = NO;}}- (void)layoutSubviews {// 設(shè)置按鈕的frame [super layoutSubviews];CGFloat btnW = 74;CGFloat btnH = 74;CGFloat margin = (self.frame.size.width - (KTotalCols * btnW)) / (KTotalCols + 1);for (int i = 0; i < self.subviews.count; i++) {CGFloat row = i / KTotalCols; // 行號CGFloat col = i % KTotalCols; // 列號CGFloat btnX = margin + col * (btnW + margin);CGFloat btnY = margin + row * (btnH + margin);UIButton *btn = self.subviews[i];btn.frame = CGRectMake(btnX, btnY, btnW, btnH);} }- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {// 得到觸摸點CGPoint startPoint = [self getCurrentPointWithTouchPoint:touches];// 判斷觸摸的位置是否在按鈕的范圍內(nèi)UIButton *btn = [self getSelectedBtnWithCurrentPoint:startPoint];// 判斷該按鈕是否存在,存儲按鈕if (btn) {// 設(shè)置選中狀態(tài)btn.selected = YES;// 將按鈕保存到數(shù)組中 [self.btns addObject:btn];} }- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {// 得到觸摸點CGPoint movePoint = [self getCurrentPointWithTouchPoint:touches];// 判斷觸摸的位置是否在按鈕的范圍內(nèi)UIButton *btn = [self getSelectedBtnWithCurrentPoint:movePoint];// 記錄當前手指移動的位置self.currentPoint = movePoint;// 判斷該按鈕是否存在和是否已選中, 存儲按鈕if (btn && btn.selected == NO) {// 設(shè)置選中狀態(tài)btn.selected = YES;// 將按鈕保存到數(shù)組中 [self.btns addObject:btn];}// 通知view繪制線段 [self setNeedsDisplay]; }- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {// 取出用戶輸入的密碼NSMutableString *result = [NSMutableString string];for (UIButton *btn in self.btns) {[result appendFormat:@"%d", btn.tag];}// 通知代理,告訴代理用戶輸入的密碼if ([self.delegate respondsToSelector:@selector(lockViewDidClick:andPwd:)]) {[self.delegate lockViewDidClick:self andPwd:result];}// 清空按鈕選中狀態(tài) [self.btns makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];// 清空數(shù)組 [self.btns removeAllObjects];// 清空currentPoint// self.currentPoint = CGPointZero; [self setNeedsDisplay]; }/*** 根據(jù)系統(tǒng)傳入的UITouch集合獲取當前觸摸的點* @return 當初觸摸的點*/ - (CGPoint)getCurrentPointWithTouchPoint:(NSSet *)touches {UITouch *touch = [touches anyObject];CGPoint point = [touch locationInView:touch.view];return point; }/*** 根據(jù)觸摸點獲取觸摸到的按鈕* @return 觸摸的按鈕*/ - (UIButton *)getSelectedBtnWithCurrentPoint:(CGPoint)point {// 判斷觸摸的位置是否在按鈕的范圍內(nèi)for (UIButton *btn in self.subviews) {if (CGRectContainsPoint(btn.frame, point)) {return btn;}}return nil; }- (void)drawRect:(CGRect)rect {// 判斷是否已有選中按鈕if (self.btns.count) {// 獲取圖形上下文CGContextRef ctx = UIGraphicsGetCurrentContext();// 背景顏色是默認背景顏色的話需要// 清空上下文 CGContextClearRect(ctx, rect);// 從數(shù)組中取出所有的按鈕, 連接所有按鈕的中點for (int i = 0; i < self.btns.count; i++) {// 取出按鈕UIButton *btn = self.btns[i];if (0 == i) {CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);}else{CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);}}// 當所有的按鈕的終點都連接號之后再連接手指當前的位置 CGContextAddLineToPoint(ctx, self.currentPoint.x , self.currentPoint.y);CGContextSetLineJoin(ctx, kCGLineJoinRound);CGContextSetLineWidth(ctx, 2);[[UIColor redColor] set];CGContextStrokePath(ctx);}}@end View Code轉(zhuǎn)載于:https://www.cnblogs.com/wentianblog/p/3784796.html
總結(jié)
以上是生活随笔為你收集整理的触摸事件练习 -- 手势解锁的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分页数据的新展示方式---瀑布流
- 下一篇: 搭建ftp环境