UITouch的用法
2015-06-24 14:32
846 查看
UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得。
//这四个方法是UIResponder中得方法
// Generally, all responders which do custom touch handling should override all four of these methods.
// Your responder will receive either touchesEnded:withEvent: or touchesCancelled:withEvent: for each
// touch it is handling (those touches it received in touchesBegan:withEvent:).
// *** You must handle cancelled touches to ensure correct behavior in your application. Failure to
// do so is very likely to lead to incorrect behavior or crashes.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
前面写了几次提到UITouch,也提到了UITouch的三个代理方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
这里还有几个代理方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;//注册接受触摸事件的Control
下面写了一个简单的DEMO,触摸时,移动图片
- (void)loadView {
[super loadView];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 50.0, 45.0, 45.0)];
image.image = [UIImage imageNamed:@"1.png"];
image.tag = 100;
[self.view addSubview:image];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
UIImageView *view1 = (UIImageView*)[self.view viewWithTag:100];
CGPoint point = [touch locationInView:self.view];
CGRect frame = view1.frame;
frame.origin = point;
view1.frame = frame;
}
效果
相关文章推荐
- iOS 9: UIStackView入门
- XCUIElementTypeQueryProvider API
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
- POJ 3709 K-Anonymous Sequence
- Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
- Hive Hue的导出XLS格式,数据不全
- iOS三种视图切换 (UITabBarController,UINavigationController,模态窗口)
- Direct UI 思想阐述
- requireJS官网学习(1)
- UICollectionViewController简单操作
- Convert boolean values to strings 'Yes' or 'No'.
- suid位,sgid位及setuid(),setgid()函数实测情况
- XCUIElementType API
- UITableView的上下滑动与自己添加的手势冲突
- poj2778 DNA Sequence(AC自动机+矩阵快速幂 )
- 比较height(),innerHeight(),outerHeight()和outerHeight(true)
- android.util.AndroidRuntimeException: requestFeature() must be called before adding content
- UIBezierPath 的理解 及使用
- Android API Guides - Activities - Fragments
- The type org.xmlpull.v1.XmlPullParser cannot be resolved. It is indirectly referenced from required