//02 - Pan Recognizer--UIPangesturerecognizer 操作
#import
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR]
@interface DragView : UIImageView
{
CGPoint previousLocation;
}
@end
@implementation DragView
- (id) initWithImage: (UIImage *) anImage
{
if (self = [super initWithImage:anImage])
{
self.userInteractionEnabled = YES;//与用户互动
//创建一个手势识别器
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.gestureRecognizers = @[panRecognizer];//将上面创建的手势识别器指定到当前层的默认操作器
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Promote the touched view
[self.superview bringSubviewToFront:self];
// Remember original location
previousLocation = self.center;
}
- (void) handlePan: (UIPanGestureRecognizer *) uigr
{ //记录手势移动距离
CGPoint translation = [uigr translationInView:self.superview];
self.center = CGPointMake(previousLocation.x + translation.x,
previousLocation.y + translation.y);
}
@end
@interface TestBedViewController : UIViewController
@end
//02b pangesturerecognizer的详细操作过程
@interface DragView : UIImageView
@end
@implementation DragView
- (id) initWithImage: (UIImage *) anImage
{
if (self = [super initWithImage:anImage])
{
self.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.gestureRecognizers = @[panRecognizer];
}
return self;
}
- (void) handlePan: (UIPanGestureRecognizer *) uigr
{
if (uigr.state == UIGestureRecognizerStateEnded)
{
CGPoint newCenter = CGPointMake(self.center.x + self.transform.tx, self.center.y + self.transform.ty);
self.center = newCenter;
CGAffineTransform theTransform = self.transform;
theTransform.tx = 0.0f;
theTransform.ty = 0.0f;
self.transform = theTransform;
return;
}
CGPoint translation = [uigr translationInView:self.superview];
CGAffineTransform theTransform = self.transform;
theTransform.tx = translation.x;
theTransform.ty = translation.y;
self.transform = theTransform;
}
@end
@interface TestBedViewController : UIViewController
@end
//01 - Direct Manipulation直接操作
#import
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR]
@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
startLocation = [[touches anyObject] locationInView:self];
[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);
// Set new location
self.center = newcenter;
}
@end
@interface TestBedViewController : UIViewController
@end
@implementation TestBedViewController
- (CGPoint) randomFlowerPosition
{
CGFloat halfFlower = 32.0f; // half of the size of the flower
// The flower must be placed fully within the view. Inset accordingly
CGSize insetSize = CGRectInset(self.view.bounds, 2*halfFlower, 2*halfFlower).size;
// Return a random position within the inset bounds
CGFloat randomX = random() % ((int)insetSize.width) + halfFlower;
CGFloat randomY = random() % ((int)insetSize.height) + halfFlower;
return CGPointMake(randomX, randomY);
}
- (void) layoutFlowers
{
// Move every flower into a new random place
[UIView animateWithDuration:0.3f animations: ^(){
for (UIView *flowerDragger in self.view.subviews)
flowerDragger.center = [self randomFlowerPosition];}];
}
- (void) viewDidAppear:(BOOL)animated
{
[self layoutFlowers];
}
- (void) loadView
{
[super loadView];
self.view.backgroundColor = [UIColor blackColor];
NSInteger maxFlowers = 12; // number of flowers to add
NSArray *flowerArray = @[@"blueFlower.png", @"pinkFlower.png", @"orangeFlower.png"];
// Add the flowers
for (int i = 0; i < maxFlowers; i++)
{
NSString *whichFlower = [flowerArray objectAtIndex:(random() % flowerArray.count)];
DragView *flowerDragger = [[DragView alloc] initWithImage:[UIImage imageNamed:whichFlower]];
[self.view addSubview:flowerDragger];
}
// Provide a "Randomize" button
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Randomize", @selector(layoutFlowers));
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check for any off-screen flowers and move them into place
CGFloat halfFlower = 32.0f;
CGRect targetRect = CGRectInset(self.view.bounds, halfFlower * 2, halfFlower * 2);
targetRect = CGRectOffset(targetRect, halfFlower, halfFlower);
for (UIView *flowerDragger in self.view.subviews)
if (!CGRectContainsPoint(targetRect, flowerDragger.center))
[UIView animateWithDuration:0.3f animations:
^(){flowerDragger.center = [self randomFlowerPosition];}];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
#pragma mark -
#pragma mark Application Setup
@interface TestBedAppDelegate : NSObject
{
UIWindow *window;
}
@end
@implementation TestBedAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setStatusBarHidden:YES];
[[UINavigationBar appearance] setTintColor:COOKBOOK_PURPLE_COLOR];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TestBedViewController *tbvc = [[TestBedViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tbvc];
window.rootViewController = nav;
[window makeKeyAndVisible];
return YES;
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
return retVal;
}
}
本文为徐绍群原创,转载请注明出处,徐绍群