[英]Moving UIImageView Moves Entire View
So I'm attempting to make some blocks (called obstacles) fall from the top of the screen. 因此,我试图使一些障碍物(称为障碍物)从屏幕顶部掉落。 This works, but my code moves the entire view down as well...
这可行,但是我的代码也将整个视图向下移动...
ViewController.h: ViewController.h:
@interface ViewController : UIViewController{
IBOutlet UIImageView * obstacle;
IBOutlet UIImageView * block;
NSTimer * obstacleTimer;
NSTimer * obMoveTimer;
}
-(void)obTimer;
-(void)moveOb;
ViewController.m: ViewController.m:
- (void)obTimer{
UIImageView *cloneImageView = [[UIImageView alloc] initWithImage:obstacle.image];
cloneImageView.frame = CGRectMake(0, -20, obstacle.frame.size.width, obstacle.frame.size.height);
cloneImageView.alpha = obstacle.alpha;
cloneImageView.layer.opacity = obstacle.layer.opacity;
cloneImageView.clipsToBounds = obstacle.clipsToBounds;
cloneImageView.backgroundColor = obstacle.backgroundColor;
cloneImageView.tintColor = obstacle.tintColor;
cloneImageView.contentMode = obstacle.contentMode;
cloneImageView.highlighted = obstacle.highlighted;
cloneImageView.opaque = obstacle.opaque;
cloneImageView.userInteractionEnabled = obstacle.userInteractionEnabled;
cloneImageView.multipleTouchEnabled = obstacle.multipleTouchEnabled;
cloneImageView.autoresizesSubviews = obstacle.autoresizesSubviews;
cloneImageView.layer.zPosition = obstacle.layer.zPosition+1;
cloneImageView.tag = numOb + 1;
numOb++;
cloneImageView.hidden = NO;
[obstacle.superview addSubview:cloneImageView];
}
- (void)moveOb{
for (int i = 0; i <= numOb; i++){
UIImageView *viewToAnimate = (UIImageView *)[obstacle.superview viewWithTag:i];
viewToAnimate.center = CGPointMake(viewToAnimate.center.x, viewToAnimate.center.y + 1);
}
}
- (void)viewDidLoad {
obstacleTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(obTimer) userInfo:nil repeats:YES];
obMoveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveOb) userInfo:nil repeats:YES];
obstacle.hidden = YES;
[super viewDidLoad];
}
So the blue blocks are falling down and so is the whole view. 因此,蓝色方块正在下降,整个视图也在下降。 Here is a picture:
这是一张图片:
Those tags are likely to cause you a problem. 这些标签可能会给您带来麻烦。 By default, a
UIView
has a tag
of 0. So when you called up viewWithTag
, are you sure you're getting the obstacle
you want? 默认情况下,
UIView
的tag
为0。因此,当您调用viewWithTag
,确定要遇到obstacle
吗?
I've used tags many a time, often for different things in the same view. 我已经多次使用标签,通常在同一视图中用于不同的事情。 typically for that I would
#define
a TAG_START_
macro for that purpose, and space them out, sort of like 通常,我会为此目的
#define
一个TAG_START_
宏,然后将它们TAG_START_
#define TAG_START_OBSTACLE 10000
And every time I would assign a tag, I would use 每次我分配标签时,我都会使用
NSInteger tag = indexIWantToUse + TAG_START_OBSTACLE;
And to regain the index, 为了重新获得索引,
NSInteger index = tag - TAG_START_OBSTACLE;
Though I must say, because I don't see the definition for obstacle
here, I would think there's a better way to get the container UIView
than using obstacle.superview
. 虽然我必须说,因为我看不到的定义
obstacle
在这里,我想有一个更好的方式来获得容器UIView
比使用obstacle.superview
。 Perhaps the UIViewController
's view
property? 也许
UIViewController
的view
属性? A container within that, or some IBOutlet
you can keep a pointer to? 其中的容器,或者您可以保留指向某个
IBOutlet
容器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.