本文主要是介绍8.3 Detecting Panning and Dragging Gestures,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
拖动
@interface ViewController ()
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, strong) UILabel *helloWorldLabel;
@end
@implementation ViewController
- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
NSLog(@"paramSender.view=%@",paramSender.view);
//UIGestureRecognizerStateEnded 时 x或y可能会是个不正常的值
if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
// self.helloWorldLabel.center = location;
}
}
-(UILabel*)getALable:(NSString*)text X:(NSInteger)x Y:(NSInteger)y
{
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 200, 80)];
lable.text = text;
lable.backgroundColor = [UIColor blackColor];
lable.textColor = [UIColor whiteColor];
lable.textAlignment = NSTextAlignmentCenter;
lable.userInteractionEnabled = YES;
return lable;
}
-(UIPanGestureRecognizer*)getPanGestureRecognizer
{
UIPanGestureRecognizer * pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
pgr.minimumNumberOfTouches = 1;
pgr.maximumNumberOfTouches = 1;
return pgr;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.helloWorldLabel = [self getALable:@"Hello World" X:50 Y:50];
[self.view addSubview:self.helloWorldLabel];
UILabel * lable2 = [self getALable:@"Hello world 2" X:50 Y:200];
[self.view addSubview:lable2];
/* Create the Pan Gesture Recognizer */
self.panGestureRecognizer = [self getPanGestureRecognizer];
/* Add it to the view */
[self.helloWorldLabel addGestureRecognizer:self.panGestureRecognizer];
//这里要重新创建一个新的GestrueRecognizer对象,否则self.helloWorldLabel就识别不了了
[lable2 addGestureRecognizer:[self getPanGestureRecognizer]];
// [self.view addGestureRecognizer:self.panGestureRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
由于有两个label,且都加上了PanGestureRecognizer,我们可以通知拉着他们移动
输出:
2014-04-10 10:56:48.026 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (50 50; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.041 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (97.5 49.5; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.043 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (98.5 50; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.057 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (98.5 50; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.073 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (99 51; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.089 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (99.5 51.5; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.105 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (100 52; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.121 cookbook[860:907] paramSender.view=<UILabel: 0x1f8441c0; frame = (50 200; 200 80); text = 'Hello world 2'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844c40>; layer = <CALayer: 0x1f844250>>
2014-04-10 10:56:48.124 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (100.5 52.5; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
2014-04-10 10:56:48.137 cookbook[860:907] paramSender.view=<UILabel: 0x1f8441c0; frame = (80 211; 200 80); text = 'Hello world 2'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844c40>; layer = <CALayer: 0x1f844250>>
2014-04-10 10:56:48.139 cookbook[860:907] paramSender.view=<UILabel: 0x1f842ee0; frame = (101 53; 200 80); text = 'Hello World'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1f844aa0>; layer = <CALayer: 0x1f842fc0>>
这篇关于8.3 Detecting Panning and Dragging Gestures的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!