我希望在触摸UIView时创建一个对象。但是我希望新对象能够移动而不必抬起手指。我尝试将touched事件传递给新对象,但是它不起作用。
有什么办法吗?
您必须派生UIView的子类,该子类将用作所有新生成的视图的HolderView。此外,一旦生成新视图,即使未抬起手指,新视图也会与手指一起移动。
遵循以下代码即可完成操作,稍后根据您的需要进行调整:
@interface HolderView : UIView
@property(nonatomic,retain)UIView *newView;
@end
@implementation HolderView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if (CGRectContainsPoint(self.bounds, touchPoint)) {
CGRect r = CGRectMake(touchPoint.x-50, touchPoint.y-50, 100, 100);
self.newView = [[[UIView alloc] initWithFrame:r]autorelease];
self.newView.backgroundColor= [UIColor cyanColor];
[self addSubview:self.newView];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.newView!=nil) {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
self.newView.center = touchPoint;
}
}
@end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句