【代碼筆記】給背景圖加上移動的手勢,背景圖手勢
一,工程圖。
二,。
三,代碼。
RootViewController.h
#import <UIKit/UIKit.h>@interface RootViewController : UIViewController<UIGestureRecognizerDelegate>@end
RootViewController.m
#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. //增加背景圖 [self addView]; }#pragma -mark -functions//背景圖-(void)addView{ //紅色的背景圖 UIView *parentView=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; parentView.backgroundColor=[UIColor redColor]; [self.view addSubview:parentView]; [parentView setUserInteractionEnabled:YES]; //移動的手勢 UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; panRcognize.delegate=self; [panRcognize setEnabled:YES]; [panRcognize delaysTouchesEnded]; [panRcognize cancelsTouchesInView]; [parentView addGestureRecognizer:panRcognize];}#pragma UIGestureRecognizer Handles- (void)handlePan:(UIPanGestureRecognizer *)recognizer { NSLog(@"--移動的手勢-----"); }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}