【发布时间】:2014-08-06 03:26:39
【问题描述】:
我有一个仅水平的 UIScrollView。 它包含几个 UIImageViews 和一个用于指示选择哪一个的borderView。
borderView 是一个有边框但没有内容的 UIView。
我想做的事:
当用户点击imageView时,我希望borderview可以移动到点击的imageView上并覆盖在上面进行指示。
我在代码中做了什么:
1.将带有UITapgesture事件和borderView的imageViews添加到scrollView
-(void)setStaticFilterToBar
{
_filterList = [APIHelper getStaticFilterList:_originBackgroundImage];
filterScrollView.contentSize = CGSizeMake(320,filterScrollView.contentSize.height);
filterScrollView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7f];
int xAis = 64;
for(int i=0; i<_filterList.count; i++)
{
UIImageView *filter = [[UIImageView alloc]initWithImage:[_filterList objectAtIndex:i]];
[filter setUserInteractionEnabled:YES];
[filter setFrame:CGRectMake(i * xAis,5,60,60)];
[filter setTag:i];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(filterElementTapToApplyFilter:)];
[tap setDelegate:self];
[filter addGestureRecognizer:tap];
[filterScrollView addSubview:filter];
if((i+1) * xAis > 320)
{
filterScrollView.contentSize = CGSizeMake(filterScrollView.contentSize.width + xAis,
filterScrollView.contentSize.height);
}
}
//add borderview
UIView *borderView = [[UIView alloc]init];
borderView.layer.borderColor = [UIColor redColor].CGColor;
borderView.layer.borderWidth = 3.0f;
borderView.layer.cornerRadius = 6;
[filterScrollView addSubview:borderView];
}
-(void)filterElementTapToApplyFilter:(UITapGestureRecognizer *) recognizer
{
//apply filter
[self applyFilterByUIView:recognizer.view];
//move the borderview to the tapped imageView
[self selectSubViewsFromScrollView:filterScrollView TargetFrame:recognizer.view.frame];
}
2.点击imageview,将borderview的frame值改为imageView的frame值。
(不管是使用animationwithDuration:animations:completion:还是直接设置框架)
-(void)selectSubViewsFromScrollView:(UIScrollView *)scrollView TargetFrame:(CGRect)targetFrame
{
//borderView is the last subview.
UIView *borderView = [scrollView.subviews objectAtIndex:scrollView.subviews.count-1];
NSLog(@"Before: borderView.frame:(%d,%d,%d,%d)",(int)borderView.frame.origin.x,
(int)borderView.frame.origin.y,
(int)borderView.frame.size.width,
(int)borderView.frame.size.height);
//1
borderView.frame = targetFrame;
//2 for animation
//[UIView animateWithDuration:0.3 animations:^{
// borderView.frame = targetFrame;
//}];
NSLog(@"After: borderView.frame:(%d,%d,%d,%d)",(int)borderView.frame.origin.x,
(int)borderView.frame.origin.y,
(int)borderView.frame.size.width,
(int)borderView.frame.size.height);
}
我遇到的问题:
正如我一开始所预测的那样,它工作得很好。
但是在滚动filterScrollView之后,点击imageview,borderview就不会再改变它的位置了。但是仍然正确的应用了正确的filter。
borderview的frame的值改变了,但是在屏幕中的位置没有改变。
这里发生了什么?我错过了什么吗?任何帮助将不胜感激。
注意。我使用情节提要并为所有视图使用无自动布局。
【问题讨论】:
-
为什么不给 UIImageViews 层添加边框呢?
-
因为我希望边框从这个图像视图移动到另一个动画。我不确定添加 .layer.boder 是否会产生这种效果。
-
您提供什么坐标以使 frameView 在滚动后移动?坐标应该是相对于滚动视图的内容大小而不是滚动视图的框架
-
borderView.frame = 选择 ImageView 的框架。 scrollView 的框架是神经使用。刚刚更新了代码以获取更多详细信息。
标签: ios uiscrollview subview