一,工程圖。

二,效果圖。

三,代碼。
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.}以上代碼是有關Android程序開發之給背景圖加上移動的手勢的全部內容,希望對大家有所幫助!
新聞熱點
疑難解答