有如下代碼:
int i=0;
for (UIView *view in [self subviews]) {
//NSLog(@"index %d - i %d",index,i);
if (i==index) {
NSLog(@"index %d - i %d",index,i);
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
i++;
}
其中index是用來選擇修改視圖的變量
但是運行之後沒反應,後來改為下面代碼:
for (UIView *view in [self subviews]) {
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
這個代碼就可以正確實現,但是我需要第一個代碼也能實現,請幫忙,謝謝
設置scrollview裡全部subview的tag,然後傳遞選中視圖的tag到index變量中,需要滿足整型變量。
for(int i = 1; i <=4; i++){
UIView *myView = [UIView......]; // set your view
myView.tag = i;
[your_scrollView addSubview:myView];
}
然後進行如下
for (UIView *view in [self subviews])
{
if (view.tag == index)
{
[view setAlpha:0.3];
[view setUserInteractionEnabled:NO];
}
}
或者有一個簡單的方法獲取view[your_scrollView viewWithTag:index]