cocos TextField + ios键盘 状态监听 移动view

cocos TextField + ios键盘 状态监听 移动view

ios监听键盘回退事件

在appcontroller.mm文件,添加:

这里写图片描述


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeContentViewPosition:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

派发事件到view

- (void) changeContentViewPosition:(NSNotification *)notification{
    cocos2d::EventCustom e("MyEvent");
    e.setUserData((void*)("Lidaha"));
    cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&e);
}

view监听


--监听事件(其它平台一样,用cocos自定义事件)
self.listenerCustom=cc.EventListenerCustom:create("MyEvent",function (args)
        self.TextFieldname:setDetachWithIME(true)
        self.TextFieldname:didNotSelectSelf()
    end )
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(self.listenerCustom, 1)

view调整位置

self.TextFieldname:addEventListener(function(sender,eventType)
            if eventType == 0 then
            self.TextFieldname:setPlaceHolder("")
            local fadeIn = cc.EaseSineIn:create(cc.MoveTo:create(15/60,cc.p(self.oriPos.x,self.oriPos.y+200)))
            local action = cc.Sequence:create(fadeAc , fadeIn)
            self.ui:runAction(action)
        elseif eventType == 1 then
            self.TextFieldname:setPlaceHolder("请输入名字……")
            local fadeIn = cc.EaseSineIn:create(cc.MoveTo:create(12/60,self.oriPos))
            local action = cc.Sequence:create(fadeAc , fadeIn)
            self.ui:runAction(action)
            local strName = self.TextFieldname:getString()
            UIServerManager:getInstance():checkNameIsOk(strName, 1)
        elseif eventType == 2 then
        elseif eventType == 3 then
        end
end)

你可能感兴趣的:(游戏,lua学习,cocos开发系列,cocos2d,游戏开发)