I make a custom view name "Cus_TextEdit" using UIView. File Cus_TextEdit.m
@implementation Cus_TextView
@synthesize text;
- (id) initWithFrame:(CGRect)frame textNew:(NSString*)textNew {
self = [super initWithFrame:frame];
if (self) {
[self setText:textNew];
UIColor* bg = [UIColor colorWithRed:225 green:0 blue:0 alpha:1];
self.backgroundColor = bg;
}
return self;
}
- (void)drawRect:(CGRect)rect {
self.text drawAtPoint:CGPointMake(5, 0) withFont:[myFont];
}
In file ViewControler.hi make :
@property (retain, nonatomic) UIPanGestureRecognizer* panGestureRecognizer;
- (IBAction)clickBtn:(id)sender;
When i click a button on main view , function clickBtn will called with code(in file ViewController.m):
Cus_TextView* cus_tv = [[Cus_TextView alloc] initWithFrame: CGRectMake(10, 10, 100, 50) textNew:@"Hello"];
[cus_tv setUserInteractionEnabled:YES];
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer)];
[cus_tv addGestureRecognizer:panGesture];
[self.view addSubview:cus_tv];
Implement function panGestureRecognizer
-(void) panGestureRecognizer:(UIPanGestureRecognizer*) gesture {
NSLog(@"panGestureRecognizer called");
CGPoint newPoint = [gesture locationInView:[self view]];
[[gesture view] setCenter:newPoint];
}
When i run app,click button, text "Hello" displayed. But when i click it, panGestureRecognizer function not be call.
You have missed :
in target method (panGestureRecognizer:)
. Just Use this..
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.