创建一个"添加播放器"视图 - 斯威夫特

时间:2016-05-20 11:59:09

标签: ios swift textfield

我正在创建一个游戏,玩家可以在视图控制器上添加多个玩家:

enter image description here

实际上,我可以添加一个新播放器并删除它:

enter image description here

删除播放器后会出现问题:

enter image description here

我希望玩家2和玩家3低于textField"当我移除玩家时名字。我怎么能这样做?

ViewDidLoad:

// MARK: ADD BUTTON

    let addButtonSize = CGSize(width: topPartSize.height, height: topPartSize.height)

    let addButton = UIButton(frame: CGRect(x: enterPlayerPartSize.width - addButtonSize.width, y: 0, width: addButtonSize.width, height: addButtonSize.height))
    addButton.titleLabel!.font = UIFont.fontAwesomeOfSize(addButtonSize.height / 2)
    addButton.setTitle(String.fontAwesomeIconWithName(.Plus), forState: .Normal)
    addButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    addButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 0.5), forState: .Highlighted)
    addButton.titleLabel?.textAlignment = .Center
    addButton.layer.zPosition = 3
    addButton.addTarget(self, action: #selector(PlayersViewController.addPlayer(_:)), forControlEvents: .TouchUpInside)
    enterPlayerPart.addSubview(addButton)

    // MARK: TEXT FIELD

    let textFieldSize = CGSize(width: enterPlayerPartSize.width - playerIconSize.width - addButtonSize.width, height: enterPlayerPartSize.height)

    textField = UITextField(frame: CGRect(x: playerIconSize.width, y: 0, width: textFieldSize.width, height: textFieldSize.height))
    textField.font = UIFont(name: "Avenir-Light", size: textFieldSize.height / 2)
    textField.attributedPlaceholder = NSAttributedString(string:"Name", attributes:[NSForegroundColorAttributeName: UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)])
    textField.textColor = UIColor.whiteColor()
    textField.textAlignment = NSTextAlignment.Left
    textField.returnKeyType = UIReturnKeyType.Done
    textField.delegate = self
    enterPlayerPart.addSubview(textField)

功能:

// MARK: ADD PLAYER - FUNCTION

func addPlayer(sender: UIButton) {

    if textField.text == nil || textField.text == "" {

        print("Enter player name")

    } else {

        createNewPlayer(textField.text!)

    }

}


// MARK: CREATE NEW PLAYER - FUNCTION

func createNewPlayer(playerName: String) {

    let playerPartSize = CGSize(width: screenWidth, height: screenHeight * 0.1)

    let playerPart = UIView(frame: CGRect(x: 0, y: playerPartSize.height * CGFloat(playersCount), width: playerPartSize.width, height: playerPartSize.height))
    scrollView.addSubview(playerPart)

    // PLAYER ICON

    let playerIconSize = CGSize(width: playerPartSize.height, height: playerPartSize.height)

    let playerIcon = UILabel(frame: CGRect(x: 0, y: 0, width: playerIconSize.width, height: playerIconSize.height))
    playerIcon.font = UIFont.fontAwesomeOfSize(topPartSize.height / 2)
    playerIcon.text = String.fontAwesomeIconWithName(.User)
    playerIcon.textAlignment = .Center
    playerIcon.textColor = UIColor.whiteColor()
    playerPart.addSubview(playerIcon)

    // REMOVE BUTTON

    let removeButtonSize = playerIconSize

    let removeButton = UIButton(frame: CGRect(x: playerPartSize.width - removeButtonSize.width, y: 0, width: playerIconSize.width, height: playerIconSize.height))
    removeButton.titleLabel!.font = UIFont.fontAwesomeOfSize(removeButtonSize.height / 2)
    removeButton.setTitle(String.fontAwesomeIconWithName(.Trash), forState: .Normal)
    removeButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    removeButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 0.5), forState: .Highlighted)
    removeButton.titleLabel?.textAlignment = .Center
    removeButton.addTarget(self, action: #selector(PlayersViewController.removePlayer(_:)), forControlEvents: .TouchUpInside)
    playerPart.addSubview(removeButton)

    // PLAYER NAME

    let playerNameSize = CGSize(width: playerPartSize.width - playerIconSize.width - removeButtonSize.width, height: playerPartSize.height)

    let playerNameLabel = UILabel(frame: CGRect(x: playerIconSize.width, y: 0, width: playerNameSize.width, height: playerNameSize.height))
    playerNameLabel.font = UIFont(name: "Avenir-Light", size: playerNameSize.height / 2)
    playerNameLabel.text = playerName
    playerNameLabel.textAlignment = .Left
    playerNameLabel.textColor = UIColor.whiteColor()
    playerPart.addSubview(playerNameLabel)

    // BOTTOM LINE

    let bottomLineHeight = playerPartSize.height / 50

    let bottomLine = UIView(frame: CGRect(x: 0, y: playerPartSize.height - bottomLineHeight, width: playerPartSize.width, height: bottomLineHeight))
    bottomLine.backgroundColor = UIColor(hexColor: 0x1B1B1B)
    playerPart.addSubview(bottomLine)

    // PLAYERS COUNT ++

    playersCount += 1

}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您有两种方法可以执行它:

  1. 您似乎使用了可重用的组件,因此您应该查看UITableView / UICollectionView。它将管理内存使用和组件布局

  2. 如果您不想迁移到这些组件,建议您将所有播放器视图存储在一个数组中,并在添加/删除操作后重新布局它们

  3. 类似的东西:

    var players : [UIView] = []
    func createNewPlayer(playerName: String) {
       //Do your stuff here
        players.append(newCreatedPalyerView)
        layout()
    }
    
    func layout() {
    
       var yPosition = 0
      for view in players {
          view.frame.origin.y = yPosition
          yPosition += view.frame.size.height
    
      }
    }
    
    func removePlayer(player : UIView) {
        self.players.remove(player)
        self.layout()
    }
    

答案 1 :(得分:0)

您需要管理您的方案,如下所示:

首先在一个UI中获取一个玩家的所有UIView。所以你应该在一个视图中拥有你所有的一个玩家的东西(玩家名称标签,删除按钮等)。 (我想你已经在playerPart - 那很酷)

现在你要在playerPart中直接添加scrollview我认为,如果是这样,那么你不应该这样做。 UIViewscrollview的{​​{1}}大小scrollview(与scrollview的内容大小相同),您应该将playerPart添加到该视图中。

现在,您可以在数组中获取该视图的所有子视图:

   NSArray *viewArr = [tempView subviews];

假设tempView在上面提到的滚动视图中被捕获,

你也可以通过调用相同的方法获得scrollview的子视图。

现在,例如,您在视图中有四个播放器(四个视图)(如果没有添加tempview,则为临时视图或scrollview),然后在播放器3上单击删除,然后您可以从该视图数组中获取该对象的索引。例如,

你有从

获得的总视图数组
   NSArray *viewArr = [tempView subviews];

然后你可以获得player3的索引,即view3 like,

  int index = [viewArr indexOfObject:player3];

现在创建一个在每次删除调用后调用的方法或函数,并将该索引作为该方法中的参数传递。

在该方法中,取一个for循环,从该索引开始并运行到此viewArr的最后一个对象(视图)。 Decrease y of every coming view in this for loop所以你所有被删除的视图都会被删除。

这是我在我的一个项目中使用的一种方法,用于删除视图并相应地管理其他视图:

  #pragma mark UpdateFrameOfViews
-(void)updateFrameOfViewWithTag : (int) myTag increament : (CGFloat)increament fromIndex : (int)index{


CustomSubClassView *tempView = [self.view viewWithTag:myTag];
UIScrollView *tempScroll = (UIScrollView*)[tempView superview];
NSArray *subViews = [tempView subviews];
BOOL status = NO;

for (int i = index; i <= subViews.count; i++) {

    if (i < subViews.count) {

        UIView *tempView1 = [subViews objectAtIndex:i];

        CGFloat newY = tempView1.frame.origin.y + increament;

        CGRect newFrame = CGRectMake(tempView1.frame.origin.x, newY, tempView1.frame.size.width, tempView1.frame.size.height);

        tempView1.frame = newFrame;
        status = YES;

    }
    if (index == subViews.count && i == subViews.count) {

 //            UIView *tempView1 = [subViews lastObject];
 //            
 //            CGFloat newY = tempView1.frame.origin.y + increament;
 //            
//            CGRect newFrame = CGRectMake(tempView1.frame.origin.x, newY, tempView1.frame.size.width, tempView1.frame.size.height);
 //            
//            tempView1.frame = newFrame;

        status = YES;
    }

  //  tempView1.backgroundColor = [UIColor orangeColor];




}
if (status) {

    CGRect containerFrame = CGRectMake(0, 0, self.view.frame.size.width, tempView.frame.size.height+increament);

    tempView.frame = containerFrame;
    tempScroll.contentSize = CGSizeMake(tempView.frame.size.width, tempView.frame.size.height);

   }


 }

这个方法仅供参考,因为这里的情况略有不同。

我的代码是客观的,仅供参考,只需要快速转换!!

这就是你管理它的方式!!

建议:最好在你的情况下使用UITableView !!!

希望这会有所帮助:)