ios UIImage随手势一起移动并限制移动区域
来源:互联网 发布:基于标记的分水岭算法 编辑:程序博客网 时间:2024/05/16 06:17
在处理直接操作时,设计关注点从UIViewController 转移到UIView。 视图,更准确地说是UIResponder ,构成了直接操作开发的核心,
通过定制从UIResponder类派生的方法可创建基于触摸的界面。
下面的代码构建UIImageView 创建一个名为DragView的子视图,并向类添加响应触摸的方法,对于图像视图,支持用户交互非常重要,
即应该将 setUserInteractionEnabled 设为 YES 这个属性影响所有视图的子视图以及视图本身。
通过更新视图中心点坐标来跟随手指的移动,当用户首次触摸任何DragView时,对象会保存开始位置到视图起点的偏移值。当用户拖动对象时,
视图将手指一同移动,始终维持相同的起点偏移值。以便让移动感觉更加自然。移动动作时通过更新对象中心点坐标发生的。
//创建一个可拖动视图
#define MAXOBJECTS 12
#define SIDELENGTH 64
#define INSET_AMT 4
#define RANDLEVEL ((random() % 128) / 256.0f)
@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];
}
- (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);
//设置移动的区域
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
@interface TestBedViewController : UIViewController
@end
@implementation TestBedViewController
CGPoint randomPoint()
{
int half = 32; // half of flower size
int freesize = 240 - 2 * half; // inner area
return CGPointMake(random() % freesize + half, random() % freesize + half);
}
//创建图像
- (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;
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
srandom(time(0));
// Add backdrop which will bound the movement for the flowers
UIView *backdrop = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 282.0f)];
backdrop.backgroundColor = [UIColor blackColor];
backdrop.center = CGPointMake(160.0f, 140.0f);
int i = 0;
// Add the flowers to random points on the screen
for ( i ; i < MAXOBJECTS; i++)
{
DragView *dragger = [[DragView alloc] initWithImage:[self createImage]];
dragger.center = randomPoint();
dragger.userInteractionEnabled = YES;
// Uncomment to see the actual view bounds
// dragger.backgroundColor = [UIColor lightGrayColor];
[backdrop addSubview:dragger];
[dragger release];
}
[self.view addSubview:backdrop];
[backdrop release];
}
@end
- ios UIImage随手势一起移动并限制移动区域
- 自定义view圆并随手指移动
- ios的视图随手指移动
- 限制鼠标移动范围在圆形区域
- 圆形随手指移动
- IOS:iPhone、iPad 拖动 UIImageView(随手指移动)
- 在delphi中如何限制鼠标的移动区域。
- 移动端 例子div随手指移动
- 移动端div随手指移动
- UIView随手指的移动
- android 随手指移动划线
- UI04_UITouch(视图随手移动效果)
- 控件随手指移动动画
- XMG View随手指移动
- 控制控件随手指移动
- Android 随手指移动小球
- Android游戏中人物随手势移动例子
- ArcGIS for IOS 地图移动至某点或区域(个人)
- ImageView与ImageButton
- Spring Quartz定时详解
- VC6.0(SP6)编译xvid
- 编译GCC及其多版本并存控制
- 如何成为一名优秀的C程序员
- ios UIImage随手势一起移动并限制移动区域
- onSaveInstanceState
- VS2010 CLR20r3错误的解决
- 【Android Training - 01】详解Activity生命周期 [ Lesson 2 - 暂停与恢复Activity ]
- shell 杂记
- 为视图加边框
- YC Interview常见问题
- SurfaceView和View
- TimeUtils