纯代码frame位移和伸缩

本文介绍了一个iOS应用中实现视图平移和伸缩功能的方法。通过定义按钮来控制视图的位置变化,并使用UIView动画进行平滑过渡。同时,文章还展示了如何根据视图位置动态启用或禁用控制按钮。
  1 //
  2 //  LWTViewController.m
  3 //  纯代码位移和伸缩作业
  4 //
  5 //  Created by apple on 14-5-20.
  6 //  Copyright (c) 2014年 lwt. All rights reserved.
  7 //
  8 
  9 #import "LWTViewController.h"
 10 #pragma mark 定义常量
 11 /** 图片距离顶部的高度 */
 12 #define KImageY 60
 13 /** 图片的尺寸 */
 14 #define KImageWidth 100
 15 /** 按钮的尺寸 */
 16 #define KMoveButton 40
 17 /** 图片平移的距离 */
 18 #define KMovingDelta 20
 19 
 20 /** 定义平移tag的枚举 */
 21 typedef enum {
 22     KMoveDirTop = 1,
 23     KMoveDirLeft,
 24     KMoveDirBottom,
 25     KMoveDirRight
 26 } KMoveDir;
 27 
 28 @interface LWTViewController ()
 29 
 30 /** 图片的属性 */
 31 @property (nonatomic, strong) UIButton *imageView;
 32 
 33 /** 平移按钮的属性 */
 34 @property (nonatomic, strong) UIButton *topBtn;
 35 @property (nonatomic, strong) UIButton *leftBtn;
 36 @property (nonatomic, strong) UIButton *bottomBtn;
 37 @property (nonatomic, strong) UIButton *rightBtn;
 38 
 39 @end
 40 
 41 @implementation LWTViewController
 42 
 43 
 44 - (void)viewDidLoad
 45 {
 46     [super viewDidLoad];
 47     // Do any additional setup after loading the view, typically from a nib.
 48     
 49     // 创建要移动的图片按钮
 50     UIButton *imageViewButton = [[UIButton alloc] init];
 51     // 按钮位置
 52     CGFloat imageX = (self.view.frame.size.width - KImageWidth) / 2;
 53     imageViewButton.frame = CGRectMake(imageX, KImageY, KImageWidth, KImageWidth);
 54     
 55     // 按钮默认背景
 56     UIImage *image = [UIImage imageNamed:@"btn_01"];
 57     [imageViewButton setBackgroundImage:image forState:UIControlStateNormal];
 58     
 59     // 按钮默认文字
 60     [imageViewButton setTitle:@"点我啊" forState:UIControlStateNormal];
 61     [imageViewButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
 62     
 63     // 按钮高亮背景
 64     UIImage *imageHighed = [UIImage imageNamed:@"btn_02"];
 65     [imageViewButton setBackgroundImage:imageHighed forState:UIControlStateHighlighted];
 66     
 67     // 按钮高亮文字
 68     [imageViewButton setTitle:@"点我干啥" forState:UIControlStateHighlighted];
 69     [imageViewButton setTitleColor:[UIColor magentaColor] forState:UIControlStateHighlighted];
 70     
 71     [self.view addSubview:imageViewButton];
 72     
 73     _imageView = imageViewButton;
 74     
 75     // 创建移动按钮
 76     
 77     //
 78     UIButton *topButton = [self createButton:@"top" andTag:KMoveDirTop andX:60 andY:320];
 79     // 监听点击事件
 80     [topButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
 81     // 属性赋值
 82     self.topBtn = topButton;
 83     
 84     //
 85     UIButton *leftButton = [self createButton:@"left" andTag:KMoveDirLeft andX:20 andY:360];
 86     // 监听点击事件
 87     [leftButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
 88     // 属性赋值
 89     self.leftBtn = leftButton;
 90     
 91     //
 92     UIButton *bottomButton = [self createButton:@"bottom" andTag:KMoveDirBottom andX:60 andY:400];
 93     // 监听点击事件
 94     [bottomButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
 95     // 属性赋值
 96     self.bottomBtn = bottomButton;
 97     
 98     //
 99     UIButton *rightButton = [self createButton:@"right" andTag:KMoveDirRight andX:100 andY:360];
100     // 监听点击事件
101     [rightButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
102     // 属性赋值
103     self.rightBtn = rightButton;
104     
105     // 创建伸缩按钮
106     // 放大
107     UIButton *plusButton = [self createButton:@"plus" andTag:1 andX:180 andY:320];
108     // 监听点击事件
109     [plusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside];
110     
111     // 缩小
112     UIButton *minusButton = [self createButton:@"minus" andTag:0 andX:240 andY:320];
113     // 监听点击事件
114     [minusButton addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside];
115     
116 }
117 
118 #pragma mark - 实现方法
119 /** 创建按钮 默认和高亮的图片名称相近,x,y是位置是手动布局的 */
120 - (UIButton *) createButton:(NSString *)location andTag: (int)tag andX: (CGFloat)x andY: (CGFloat)y
121 {
122     UIButton *btn = [[UIButton alloc] init];
123     
124     btn.frame = CGRectMake(x, y, KMoveButton, KMoveButton);
125     // 背景
126     NSString *normal = [NSString stringWithFormat:@"%@_normal",location];
127     UIImage *upImage = [UIImage imageNamed:normal];
128     [btn setBackgroundImage:upImage forState:UIControlStateNormal];
129     
130     //高亮背景
131     NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted",location];
132     UIImage *upImageHighed = [UIImage imageNamed:highlighted];
133     [btn setBackgroundImage:upImageHighed forState:UIControlStateHighlighted];
134     // tag
135     btn.tag = tag;
136     [self.view addSubview:btn];
137     return btn;
138 }
139 
140 
141 /** 创建动画效果 */
142 - (void) makeAnimation : (void (^)())block
143 {
144     [UIView beginAnimations:nil context:nil];
145     [UIView setAnimationDuration:1.0];
146     
147     block();
148     
149     [UIView commitAnimations];
150 }
151 
152 /** 平移方法 */
153 - (void) move : (UIButton *)button
154 {
155     [self makeAnimation:^{
156         // 创建一个临时的CGRect
157         CGRect tempFrame = self.imageView.frame;
158         
159         // 判断平移方向
160         switch (button.tag) {
161             case KMoveDirTop: //
162                 tempFrame.origin.y -= KMovingDelta;
163                 // 平移不能超过手机屏幕
164                 if (tempFrame.origin.y < 0) {
165                     tempFrame.origin.y = 0;
166                 }
167                 break;
168             case KMoveDirLeft: //
169                 tempFrame.origin.x -= KMovingDelta;
170                 // 平移不能超过手机屏幕
171                 if (tempFrame.origin.x < 0) {
172                     tempFrame.origin.x = 0;
173                 }
174                 break;
175             case KMoveDirBottom: //
176                 tempFrame.origin.y += KMovingDelta;
177                 // 平移不能超过手机屏幕
178                 if (tempFrame.origin.y > (self.view.frame.size.height - KImageWidth)) {
179                     tempFrame.origin.y = self.view.frame.size.height -KImageWidth;
180                 }
181                 break;
182             case KMoveDirRight: //
183                 tempFrame.origin.x += KMovingDelta;
184                 // 平移不能超过手机屏幕
185                 if (tempFrame.origin.x > (self.view.frame.size.width - KImageWidth)) {
186                     tempFrame.origin.x = self.view.frame.size.width - KImageWidth;
187                 }
188                 break;
189         }
190         
191         // 重新赋值
192         self.imageView.frame = tempFrame;
193         
194         // 平移到与屏幕重合则无法点击按钮 不用三目运算符则会出现一直无法点击事件
195         self.topBtn.enabled = self.imageView.frame.origin.y ? 1 : 0;
196         self.leftBtn.enabled = self.imageView.frame.origin.x ? 1 : 0;
197         self.bottomBtn.enabled = self.imageView.frame.origin.y - (self.view.frame.size.height -KImageWidth) ? 1 : 0;
198         self.rightBtn.enabled = self.imageView.frame.origin.x - (self.view.frame.size.width - KImageWidth) ? 1 : 0;
199 
200     }];
201     
202 }
203 
204 /** 伸缩方法 */
205 - (void) scale : (UIButton *)button
206 {
207     [self makeAnimation:^{
208         // 创建临时CGRect
209         CGRect tempBounds = self.imageView.bounds;
210         // 判断放大缩小
211         if (button.tag) {
212             tempBounds.size.width *= 1.2 ;
213             tempBounds.size.height *= 1.2;
214         } else {
215             tempBounds.size.width *= 0.8;
216             tempBounds.size.height *= 0.8;
217         }
218         // 重新赋值
219         self.imageView.bounds = tempBounds;
220     }];
221 }
222 
223 
224 - (void)didReceiveMemoryWarning
225 {
226     [super didReceiveMemoryWarning];
227     // Dispose of any resources that can be recreated.
228 }
229 
230 @end

图片自己找

转载于:https://www.cnblogs.com/wentianblog/p/3742599.html

### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向速度。在数学上,光流场可以表示为像素位置时间的一阶导数,即Ex、Ey(空间梯度)Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函数负责计算光流场。该函数接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey``Et`分别代表沿x轴、y轴时间轴的像素强度变化;`gray1``gray2`用于存储当前帧前一帧的平均灰度值;`u`则表示计算出的光流矢量大小。 #### 图像处理流程 1. **初始化预处理**:`memset`函数被用来清零`opticalflow`数组,它将保存计算出的光流数据。同时,`output`数组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性效率。 3. **光流向量计算**:通过比较当前帧前一帧的灰度值,计算出每个像素点的Ex、EyEt值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算出的光流值被存储在`opticalflow`数组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`数组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函数将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函数中,通过调用`cvCalcOpticalFlowLK`函数实现了这一算法,该函数接受前一帧当前帧的灰度图,以及窗口大小等参数,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解优化光流算法,可以进一步提升视频分析的准确性实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉理解动态场景中的运动信息
微信小程序作为腾讯推出的一种轻型应用形式,因其便捷性与高效性,已广泛应用于日常生活中。以下为该平台的主要特性及配套资源说明: 特性方面: 操作便捷,即开即用:用户通过微信内搜索或扫描二维码即可直接使用,无需额外下载安装,减少了对手机存储空间的占用,也简化了使用流程。 多端兼容,统一开发:该平台支持在多种操作系统与设备上运行,开发者无需针对不同平台进行重复适配,可在一个统一的环境中完成开发工作。 功能丰富,接口完善:平台提供了多样化的API接口,便于开发者实现如支付功能、用户身份验证及消息通知等多样化需求。 社交整合,传播高效:小程序深度嵌入微信生态,能有效利用社交关系链,促进用户之间的互动与传播。 开发成本低,周期短:相比传统应用程序,小程序的开发投入更少,开发周期更短,有助于企业快速实现产品上线。 资源内容: “微信小程序-项目源码-原生开发框架-含效果截图示例”这一资料包,提供了完整的项目源码,并基于原生开发方式构建,确保了代码的稳定性与可维护性。内容涵盖项目结构、页面设计、功能模块等关键部分,配有详细说明与注释,便于使用者迅速理解并掌握开发方法。此外,还附有多个实际运行效果的截图,帮助用户直观了解功能实现情况,评估其在实际应用中的表现与价值。该资源适用于前端开发人员、技术爱好者及希望拓展业务的机构,具有较高的参考与使用价值。欢迎查阅,助力小程序开发实践。资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值