我希望在触摸UIView时创建一个对象。但我希望新对象能够移动而不必抬起手指。我尝试将触摸的事件传递给新对象,但它不起作用。
有什么办法吗?
答案 0 :(得分:0)
您必须派生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
答案 1 :(得分:0)
我认为触摸会在您触摸其他对象时产生问题视图也存在,因此您的可移动对象上无法识别触摸,因为您需要在UIView上使用一个UIImageview然后检测触摸,然后你就能实现所需的输出
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint imgPop = [touch locationInView:imgPopup];
if ([imgPopup pointInside:imgPop withEvent:event])
{
CGPoint imgReply = [touch locationInView:viewComment];
if ([viewComment pointInside:imgReply withEvent:event])
{
} else {
viewPopup.hidden = TRUE;
}
}
}
这就是我管理此代码的方法是解除触摸视图,但您可以修改并用于您的目的。