告诉ViewController在第一个图像被拖动后移动第二个图像

时间:2013-08-13 23:11:53

标签: ios uiimageview

我创建了一个名为DragImageView的自定义UIImageView类,它允许在屏幕上拖动图像,其中包含第一次和最后一次触摸的动画。在我的ViewController中,我有几个不同的图像与这个类。我想要做的是,一旦第一个图像被拖放,第二个图像将转换为第一个图像的初始起点。可以拖动第二个图像,一旦它被释放,第三个图像转换为相同的起始点,依此类推。目前,在我拖动图像后,我必须重新触摸屏幕才能翻译下一个图像。我希望它在用户完成拖动后自动翻译。任何帮助/建议都会很棒。

//在DragImageView.h中

#import <UIKit/UIKit.h>

@interface DragImageView : UIImageView
{
    CGPoint startLocation;
    CGPoint endLocation;
}

@property (nonatomic) BOOL complete; // Hoping this will tell ViewController to move next image

@end

//在DragImageView.m

#import "DragImageView.h"

@implementation DragImageView

@synthesize complete;

- (id) initWithImage: (UIImage *) anImage
{
    if (self = [super initWithImage:anImage])
        self.complete = NO;
    return self;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    startLocation = [[touches anyObject] locationInView:self.superview];
    [self animateFirstTouchAtPoint:startLocation];
}

- (void)animateFirstTouchAtPoint:(CGPoint)touchPoint{
#define GROW_FACTOR 1.4f
#define SHRINK_FACTOR 1.0f
#define GROW_ANIMATION_DURATION_SECONDS 0.15

    NSValue *touchPointValue = [NSValue valueWithCGPoint:touchPoint];
    [UIView beginAnimations:nil context:(__bridge_retained void *)touchPointValue];
    [UIView setAnimationDuration:GROW_ANIMATION_DURATION_SECONDS];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop:finished:context:)];
    CGAffineTransform transform = CGAffineTransformMakeScale(GROW_FACTOR, GROW_FACTOR);
    self.transform = transform;
    [UIView commitAnimations];
}

- (void)growAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

#define MOVE_ANIMATION_DURATION_SECONDS 0.25

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:MOVE_ANIMATION_DURATION_SECONDS];
    self.transform = CGAffineTransformMakeScale(SHRINK_FACTOR, SHRINK_FACTOR);
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    self.center = pt;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    endLocation = [[touches anyObject] locationInView:self.superview];
    [self animateLastTouchAtPoint:endLocation];
    self.complete = YES;
}

- (void) animateLastTouchAtPoint: (CGPoint) touchPoint
{
    NSValue *lastTouchPointValue = [NSValue valueWithCGPoint:touchPoint];
    [UIView beginAnimations:nil context:(__bridge_retained void *)lastTouchPointValue];
    [UIView setAnimationDuration:0.25f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.alpha = 0.0;
    [UIView commitAnimations];
}

@end

//在ViewController.h中

#import <UIKit/UIKit.h>
#import "DragImageView.h"

@class DragImageView;

@interface ViewController : UIViewController
{
    CGPoint startPoint;
}

@property (nonatomic, strong) IBOutlet DragImageView *baseball;
@property (nonatomic, strong) IBOutlet DragImageView *basketball;
@property (nonatomic, strong) IBOutlet DragImageView *soccerBall;

- (void) moveBasketball;
- (void) moveSoccerBall;

@end

//在ViewController.m中

#import "ViewController.h"

@implementation ViewController

@synthesize baseball, basketball, soccerBall;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.baseball.userInteractionEnabled = YES;
    startPoint = CGPointMake(self.baseball.center.x, self.baseball.center.y);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) moveBasketball
{
    self.basketball.center = startPoint;
}

- (void) moveSoccerBall
{
    self.soccerBall.center = startPoint;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.baseball.complete == YES)
    {
        [self moveBasketball];
        self.basketball.userInteractionEnabled = YES;
    }

    if (self.baseball.complete == YES && self.basketball.complete == YES)
    {
        [self moveSoccerBall];
        self.soccerBall.userInteractionEnabled = YES;
    }

@end

1 个答案:

答案 0 :(得分:0)

你应该切换到新的基于块的动画,这是Apple自iOS 4以来推荐的动画。这些方法有一个完成块,所以你可以在动画最后一次触摸结束时开始你的下一个动画。