我创建了一个名为 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