ios UIImage随手势一起移动并限制移动区域

来源:互联网 发布:基于标记的分水岭算法 编辑:程序博客网 时间:2024/05/16 06:17

在处理直接操作时,设计关注点从UIViewController 转移到UIView。 视图,更准确地说是UIResponder ,构成了直接操作开发的核心,

通过定制从UIResponder类派生的方法可创建基于触摸的界面。

下面的代码构建UIImageView 创建一个名为DragView的子视图,并向类添加响应触摸的方法,对于图像视图,支持用户交互非常重要,

即应该将 setUserInteractionEnabled 设为 YES 这个属性影响所有视图的子视图以及视图本身。

通过更新视图中心点坐标来跟随手指的移动,当用户首次触摸任何DragView时,对象会保存开始位置到视图起点的偏移值。当用户拖动对象时,

视图将手指一同移动,始终维持相同的起点偏移值。以便让移动感觉更加自然。移动动作时通过更新对象中心点坐标发生的。


//创建一个可拖动视图


#define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]

#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);


//设置移动的区域

    // 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


@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