IOS之手势锁屏

在iOS上增加手势锁屏、解锁功能

具体的代码实现如下:

  1. //  
  2. //  ViewController.m  
  3. //  GestureLock  
  4. //  
  5. //  Created by Jason Lee on 12-9-26.  
  6. //  Copyright (c) 2012年 Jason Lee. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. #define LOCK_POINT_TAG      1000  
  12.   
  13. @interface ViewController ()  
  14.   
  15. @property (nonatomic, strong) UIImageView *imageView;  
  16.   
  17. @property (nonatomic, assign) CGPoint lineStartPoint;  
  18. @property (nonatomic, assign) CGPoint lineEndPoint;  
  19.   
  20. @property (nonatomic, strong) NSMutableArray *buttonArray;  
  21. @property (nonatomic, strong) NSMutableArray *selectedButtons;  
  22.   
  23. @property (nonatomic, assign) BOOL drawFlag;  
  24.   
  25. @property (nonatomic, strong) UIImage *pointImage;  
  26. @property (nonatomic, strong) UIImage *selectedImage;  
  27.   
  28. @end  
  29.   
  30. @implementation ViewController  
  31.   
  32. - (void)dealloc  
  33. {  
  34.     [super dealloc];  
  35.     //  
  36.     [_imageView release];  
  37.     [_buttonArray release];  
  38.     [_selectedButtons release];  
  39.     [_pointImage release];  
  40.     [_selectedImage release];  
  41. }  
  42.   
  43. - (void)viewDidLoad  
  44. {  
  45.     [super viewDidLoad];  
  46.     // Do any additional setup after loading the view, typically from a nib.  
  47.       
  48.     _imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];  
  49.     [self.view addSubview:self.imageView];  
  50.     self.imageView.backgroundColor = [UIColor whiteColor];  
  51.       
  52.     [self createLockPoints];  
  53. }  
  54.   
  55. - (void)didReceiveMemoryWarning  
  56. {  
  57.     [super didReceiveMemoryWarning];  
  58.     // Dispose of any resources that can be recreated.  
  59. }  
  60.   
  61. #pragma mark - Trace Touch Point  
  62.   
  63. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  64. {  
  65.     UITouch *touch = [touches anyObject];  
  66.     if (touch) {  
  67.         for (UIButton *btn in self.buttonArray) {  
  68.             CGPoint touchPoint = [touch locationInView:btn];  
  69.             if ([btn pointInside:touchPoint withEvent:nil]) {  
  70.                 self.lineStartPoint = btn.center;  
  71.                 self.drawFlag = YES;  
  72.                   
  73.                 if (!self.selectedButtons) {  
  74.                     self.selectedButtons = [NSMutableArray arrayWithCapacity:9];  
  75.                 }  
  76.                   
  77.                 [self.selectedButtons addObject:btn];  
  78.                 [btn setImage:self.selectedImage forState:UIControlStateNormal];  
  79.             }  
  80.         }  
  81.     }  
  82. }  
  83.   
  84. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  85. {  
  86.     UITouch *touch = [touches anyObject];  
  87.     if (touch && self.drawFlag) {  
  88.         self.lineEndPoint = [touch locationInView:self.imageView];  
  89.           
  90.         for (UIButton *btn in self.buttonArray) {  
  91.             CGPoint touchPoint = [touch locationInView:btn];  
  92.               
  93.             if ([btn pointInside:touchPoint withEvent:nil]) {  
  94.                 BOOL btnContained = NO;  
  95.                   
  96.                 for (UIButton *selectedBtn in self.selectedButtons) {  
  97.                     if (btn == selectedBtn) {  
  98.                         btnContained = YES;  
  99.                         break;  
  100.                     }  
  101.                 }  
  102.                   
  103.                 if (!btnContained) {  
  104.                     [self.selectedButtons addObject:btn];  
  105.                     [btn setImage:self.selectedImage forState:UIControlStateNormal];  
  106.                 }  
  107.             }  
  108.         }  
  109.           
  110.         self.imageView.image = [self drawUnlockLine];  
  111.     }  
  112. }  
  113.   
  114. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  115. {  
  116.     [self outputSelectedButtons];  
  117.       
  118.     self.drawFlag = NO;  
  119.     self.imageView.image = nil;  
  120.     self.selectedButtons = nil;  
  121. }  
  122.   
  123. - (void)createLockPoints  
  124. {  
  125.     self.pointImage = [UIImage imageNamed:@"blue_circle"];  
  126.     self.selectedImage = [UIImage imageNamed:@"yellow_circle"];  
  127.       
  128.     float marginTop = 100;  
  129.     float marginLeft = 45;  
  130.       
  131.     float y;  
  132.     for (int i = 0; i < 3; ++i) {  
  133.         y = i * 100;  
  134.         float x;  
  135.         for (int j = 0; j < 3; ++j) {  
  136.             x = j * 100;  
  137.             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  138.             [btn setImage:self.pointImage forState:UIControlStateNormal];  
  139.             [btn setImage:self.selectedImage forState:UIControlStateHighlighted];  
  140.             btn.frame = (CGRect){x+marginLeft, y+marginTop, self.pointImage.size};  
  141.             [self.imageView addSubview:btn];  
  142.             btn.userInteractionEnabled = NO;  
  143.             btn.tag = LOCK_POINT_TAG + i * 3 + j;  
  144.               
  145.             if (!self.buttonArray) {  
  146.                 self.buttonArray = [NSMutableArray arrayWithCapacity:9];  
  147.             }  
  148.             [self.buttonArray addObject:btn];  
  149.         }  
  150.     }  
  151. }  
  152.   
  153. #pragma mark - Draw Line  
  154.   
  155. - (UIImage *)drawUnlockLine  
  156. {  
  157.     UIImage *image = nil;  
  158.       
  159.     UIColor *color = [UIColor yellowColor];  
  160.     CGFloat width = 5.0f;  
  161.     CGSize imageContextSize = self.imageView.frame.size;  
  162.       
  163.     UIGraphicsBeginImageContext(imageContextSize);  
  164.       
  165.     CGContextRef context = UIGraphicsGetCurrentContext();  
  166.       
  167.     CGContextSetLineWidth(context, width);  
  168.     CGContextSetStrokeColorWithColor(context, [color CGColor]);  
  169.       
  170.     CGContextMoveToPoint(context, self.lineStartPoint.x, self.lineStartPoint.y);  
  171.     for (UIButton *selectedBtn in self.selectedButtons) {  
  172.         CGPoint btnCenter = selectedBtn.center;  
  173.         CGContextAddLineToPoint(context, btnCenter.x, btnCenter.y);  
  174.         CGContextMoveToPoint(context, btnCenter.x, btnCenter.y);  
  175.     }  
  176.     CGContextAddLineToPoint(context, self.lineEndPoint.x, self.lineEndPoint.y);  
  177.       
  178.     CGContextStrokePath(context);  
  179.       
  180.     image = UIGraphicsGetImageFromCurrentImageContext();  
  181.       
  182.     UIGraphicsEndImageContext();  
  183.       
  184.     return image;  
  185. }  
  186.   
  187. #pragma mark -   
  188.   
  189. - (void)outputSelectedButtons  
  190. {  
  191.     for (UIButton *btn in self.selectedButtons) {  
  192.         [btn setImage:self.pointImage forState:UIControlStateNormal];  
  193.         NSLog(@"Selected-button's tag : %d\n", btn.tag);  
  194.     }  
  195. }  
  196.   
  197. @end  
考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值