长沙不孕不育医院,李雨桐回应薛之谦,天使之争男主角
预备知识
ios处理屏幕上的触摸动作,主要涉及到以下几个方法:
如何实现拖动视图?
1.设置userinteractionenabled属性为yes,允许用户交互。
2.在触摸动作开始时记录起始点。
3.在移动过程中,计算当前位置坐标与起始点的差值,即偏移量,并且移动视图中心点至偏移量大小的地方。
4.分别限制x坐标、与y坐标,保证用户不可将视图托出屏幕
备注:分别限制x坐标与y坐标的原因是,即使向右拖动不了了,仍需保证可以向下拖动。
其实,功能比较简单,就是ios手势动画中的拖动。来看一下基本的写法:
1.注册拖动动画
注:vlight就是要加入拖动的view子类。
2.拖动处理函数
}
实现代码
以子类化uiimageview为例
#import <uikit/uikit.h>
@interface gragview : uiimageview
{
cgpoint startpoint;
}
@end
#import "gragview.h"
@implementation gragview
- (id)initwithframe:(cgrect)frame
{
self = [super initwithframe:frame];
if (self) {
// initialization code
//允许用户交互
self.userinteractionenabled = yes;
}
return self;
}
- (id)initwithimage:(uiimage *)image
{
self = [super initwithimage:image];
if (self) {
//允许用户交互
self.userinteractionenabled = yes;
}
return self;
}
- (void) touchesbegan:(nsset *)touches withevent:(uievent *)event
{
//保存触摸起始点位置
cgpoint point = [[touches anyobject] locationinview:self];
startpoint = point;
//该view置于最前
[[self superview] bringsubviewtofront:self];
}
-(void) touchesmoved:(nsset *)touches withevent:(uievent *)event
{
//计算位移=当前位置-起始位置
cgpoint point = [[touches anyobject] locationinview:self];
float dx = point.x - startpoint.x;
float dy = point.y - startpoint.y;
//计算移动后的view中心点
cgpoint newcenter = cgpointmake(self.center.x + dx, self.center.y + dy);
/* 限制用户不可将视图托出屏幕 */
float halfx = cgrectgetmidx(self.bounds);
//x坐标左边界
newcenter.x = max(halfx, newcenter.x);
//x坐标右边界
newcenter.x = min(self.superview.bounds.size.width - halfx, newcenter.x);
//y坐标同理
float halfy = cgrectgetmidy(self.bounds);
newcenter.y = max(halfy, newcenter.y);
newcenter.y = min(self.superview.bounds.size.height - halfy, newcenter.y);
//移动view
self.center = newcenter;
}
/*
// only override drawrect: if you perform custom drawing.
// an empty implementation adversely affects performance during animation.
- (void)drawrect:(cgrect)rect
{
// drawing code
}
*/
@end
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
iOS 使用UITextField自定义搜索框 实现用户输入完之后“实时搜索”功能
网友评论