如果要VIEW支持用户交互,得要设置
self.userInteractionEnabled = YES;
一个简单的UIView例子
@interface DragView : UIImageView
{
CGPoint startLocation;
}
@end
@implementation DragView
- (id) initWithImage: (UIImage *) anImage
{
if (self = [super initWithImage:anImage])
self.userInteractionEnabled = YES;
return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// Calculate and store offset, and pop view into front if needed
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
[[self superview] bringSubviewToFront:self];
NSLog(@"begin");
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Calculate offset
CGPoint pt = [[touches anyObject] locationInView:self];
float dx = pt.x - startLocation.x;
float dy = pt.y - startLocation.y;
CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
// Bound movement into parent bounds
float halfx = CGRectGetMidX(self.bounds);
newcenter.x = MAX(halfx, newcenter.x);
newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
float halfy = CGRectGetMidY(self.bounds);
newcenter.y = MAX(halfy, newcenter.y);
newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
// Set new location
self.center = newcenter;
}
@end
手动创建一图片的UIImageView
- (UIImage *) createImage
{
UIColor *color = [UIColor colorWithRed:RANDLEVEL green:RANDLEVEL blue:RANDLEVEL alpha:1.0f];
//设置图片的区域
UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH));
CGContextRef context = UIGraphicsGetCurrentContext();
// Create a filled ellipse 填充区域
[color setFill];
CGRect rect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);
CGContextAddEllipseInRect(context, rect);
CGContextFillPath(context);
// Outline the circle a couple of times
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextAddEllipseInRect(context, CGRectInset(rect, INSET_AMT, INSET_AMT));
CGContextStrokePath(context);
CGContextAddEllipseInRect(context, CGRectInset(rect, 2*INSET_AMT, 2*INSET_AMT));
CGContextStrokePath(context);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
判断是否在区域里面 HALFSIDE是圆的半径
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGPoint pt;
float HALFSIDE = SIDELENGTH / 2.0f;
// normalize with centered origin
pt.x = (point.x - HALFSIDE) / HALFSIDE;
pt.y = (point.y - HALFSIDE) / HALFSIDE;
// x^2 + y^2 = radius
float xsquared = pt.x * pt.x;
float ysquared = pt.y * pt.y;
// If the radius < 1, the point is within the clipped circle
if ((xsquared + ysquared) < 1.0) return YES;
return NO;
}
根据图像的像位判断是否点击到图片,位图上面的触摸
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//如果点下去的点不在self.bounds里面直接返回NO
if (!CGRectContainsPoint(self.bounds, point)) {
return NO;
}
//如果这个点的中断值 ALPHA小于33%,就可以看做是透明了,但也要和实际情况
return (bytes[alphaOffset(point.x, point.y, self.image.size.width)] > 85);
}
//计算当前点在数像中的位的位置
//X,Y表示点的坐标,W表示当前VIEW的宽度
NSUInteger alphaOffset(NSUInteger x, NSUInteger y, NSUInteger w){
return y * w * 4 + x * 4 + 0;
}
//计算图像的位
unsigned char *getBitmapFromImage (UIImage *image)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
CGSize size = image.size;
// void *bitmapData = malloc(size.width * size.height * 4);
unsigned char *bitmapData = calloc(size.width * size.height * 4, 1); // Courtesy of Dirk. Thanks!
if (bitmapData == NULL)
{
fprintf (stderr, "Error: Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
CGContextRef context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace );
if (context == NULL)
{
fprintf (stderr, "Error: Context not created!");
free (bitmapData);
return NULL;
}
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGContextDrawImage(context, rect, image.CGImage);
unsigned char *data = CGBitmapContextGetData(context);
CGContextRelease(context);
return data;
}
视图持久性和归档
//持久化
- (void) updateDefaults
{
NSMutableArray *colors = [[NSMutableArray alloc] init];
NSMutableArray *locs = [[NSMutableArray alloc] init];
for (DragView *dv in [[self.view viewWithTag:201] subviews])
{
[colors addObject:dv.whichFlower];
[locs addObject:NSStringFromCGRect(dv.frame)];
}
[[NSUserDefaults standardUserDefaults] setObject:colors forKey:@"colors"];
[[NSUserDefaults standardUserDefaults] setObject:locs forKey:@"locs"];
[[NSUserDefaults standardUserDefaults] synchronize];
[colors release];
[locs release];
}
//删除
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"colors"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"locs"];
//加载
NSMutableArray *colors = [[NSUserDefaults standardUserDefaults] objectForKey:@"colors"];
NSMutableArray *locs = [[NSUserDefaults standardUserDefaults] objectForKey:@"locs"];
//归档
@interface DragView : UIImageView
{
CGPoint startLocation;
NSString *whichFlower;
}
@property (retain) NSString *whichFlower;
@end
@implementation DragView
@synthesize whichFlower;
//保存
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeCGRect:self.frame forKey:@"viewFrame"];
[coder encodeObject:self.whichFlower forKey:@"flowerType"];
}
//初始化
- (id) initWithCoder: (NSCoder *)coder
{
[super initWithFrame:CGRectZero];
self.frame = [coder decodeCGRectForKey:@"viewFrame"];
self.whichFlower = [coder decodeObjectForKey:@"flowerType"];
self.image = [UIImage imageNamed:self.whichFlower];
self.userInteractionEnabled = YES;
return self;
}
//保存
NSArray *flowers = [[self.view viewWithTag:201] subviews];
[NSKeyedArchiver archiveRootObject:flowers toFile:DATAPATH];
//加载
NSArray *flowers = [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH];
使用NSUndoManager来执行Undo和reDo操作
// Initialize the undo manager for this application
self.undoManager = [[NSUndoManager alloc] init];
//设置UNDO次数
[self.undoManager setLevelsOfUndo:999];
//注册 在视图时同注册
[[self.undoManager prepareWithInvocationTarget:self] setPosition:self.center];
//然后是否正在undo
while ([self.undoManager isUndoing]);
// Don't show the undo button if the undo stack is empty
//是否能undo
if (!self.undoManager.canUndo)
self.navigationItem.leftBarButtonItem = nil;
else
self.navigationItem.leftBarButtonItem = BARBUTTON(@"Undo", @selector(undo));
//最后再处理undo
[self.undoManager undo];
预留
预留
预留
预留
预留
分享到:
相关推荐
手势与触摸事件,基本属性和方法
NULL 博文链接:https://andydufresneone.iteye.com/blog/1074250
ios应用源码之手势和触摸 .
axure 7.0的部件库, 内有所有的触摸手势操作示意图, 划屏幕,点击等等.共100个元件.
触摸和手势 pdf ios开发必备
win7 触摸屏手势识别编程,实现整个识别过程
手势和触摸.zipIOS应用例子源码下载手势和触摸.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
由于以图像为手势信息载体的交互方式易受环境影响,设计实现了一种基于智能手机触摸屏与内置传感器的手势交互方案。首先通过手机内置MEMS传感器及触摸屏获取手势信息;然后分析手势的运动学特征,实时提取手势的信息...
这是Qt for Android的测试工程,主要测试QT的触摸手势事件
雨松MOMO Unity3D 游戏开发系列博文之Unity3D 游戏引擎之IOS触摸屏手势控制镜头旋转与缩放,欢迎大家下载阅读,哇咔咔~~
Android触摸屏手势识别,Android触摸屏手势识别,Android触摸屏手势识别
IOS应用源码之手势和触摸 .rar
IOS应用源码——手势和触摸.zip
IOS应用源码——手势和触摸.rar
基于手势识别的虚拟触摸屏系统源码+项目说明.zip基于手势识别的虚拟触摸屏系统源码+项目说明.zip基于手势识别的虚拟触摸屏系统源码+项目说明.zip基于手势识别的虚拟触摸屏系统源码+项目说明.zip基于手势识别的虚拟...
基于UiAutomator框架,自动模拟用户手势操作,类似于触摸精灵,按键精灵的app。
ios手势和触摸代码完整版.7z
触摸手势编程
Qt实现多点触控与手势识别