我想知道如何添加按钮(左侧和右侧)来更改页面控制视图。 我正在学习本教程[1]:http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/。除了此示例代码中的交换功能之外,如何添加2个简单按钮(左侧和右侧)来翻页?如何将按钮添加到页面控制器
我是一个程序员初学者,所以任何一种答案都非常appriciated! :)
谢谢!
我想知道如何添加按钮(左侧和右侧)来更改页面控制视图。 我正在学习本教程[1]:http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/。除了此示例代码中的交换功能之外,如何添加2个简单按钮(左侧和右侧)来翻页?如何将按钮添加到页面控制器
我是一个程序员初学者,所以任何一种答案都非常appriciated! :)
谢谢!
您可以将两个按钮添加到视图中,并且当单击该按钮时,请调用一种方法根据单击的按钮转动页面。
UIButton *leftButton = [[UIButton alloc] init];
leftbutton.frame = leftButtonFrame;
[leftbutton setTitle:@"Left" forState:UIControlStateNormal];
[leftbutton addTarget:self action:@selector(leftbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[yourView addSubview:leftbutton];
UIButton *rightButton = [[UIButton alloc] init];
rightButton.frame = rightButtonFrame;
[rightButton setTitle:@"Right" forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(rightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[yourView addSubview:rightButton];
- (void)leftButtonclicked:(id)sender
{
//Code to turn page left
}
- (void)rightButtonclicked:(id)sender
{
//Code to turn page right
}
我认为那不是他想知道的。他问道并指出了PageControl的教程。他想知道的是如何以编程方式翻页,而不是使用IB-Outlet。 – 2011-03-21 09:35:36
我打算在swift中回答这个问题,但你应该可以翻译。
该过程是查找PageControl视图,添加两个按钮并在适当时隐藏按钮(即第一页上没有上一个按钮)。
我将这些按钮放在PageControl的左侧和右侧。默认行为是触摸那里做回页面和页面转发。所以我将按钮设置为enabled = false,以便触摸执行此默认行为。
首先,我们需要一个枚举来帮助定位按钮。使用不会在别处使用的值。
enum enumBtnTag: Int {
case tagPrev = 9991
case tagNext = 9992
}
var pageNbr = 0 //Needed to keep track of page being displayed
现在我们将在ViewDidLoad中添加我们的按钮。首先我找到PageControl,然后查找按钮以避免创建两次。 (ViewDidload可以被多次调用)
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for view in self.view.subviews {
if view is UIPageControl {
let curr:UIPageControl = view as! UIPageControl
curr.backgroundColor = UIColor.clear
curr.currentPageIndicatorTintColor = UIColor.red //Page Dot is red
curr.pageIndicatorTintColor = UIColor.black //Other dots are black
let pcSz = view.frame
let btnSz = CGSize(width: 35, height: 50) //Use your button size
if let _ = self.view.viewWithTag(enumBtnTag.tagNext.rawValue) as? UIButton {}
else { //Next Button not found
let Nbtn = UIButton(frame: CGRect(x: pcSz.width - btnSz.width, y: -15, width: btnSz.width, height: btnSz.height))
Nbtn.setTitle(">>", for: UIControlState.normal)
Nbtn.backgroundColor = UIColor.clear
Nbtn.setTitleColor(UIColor.brown, for: UIControlState.normal)
Nbtn.titleLabel?.font = UIFont(name: enumFontNames.MarkerFelt_Wide.rawValue, size: 60.0)
Nbtn.isEnabled = false //Allows touch to fall through to PageControl
Nbtn.tag = enumBtnTag.tagNext.rawValue
view.addSubview(Nbtn)
}
if let _ = self.view.viewWithTag(enumBtnTag.tagPrev.rawValue) as? UIButton {}
else { //Prev Button not found
let Pbtn = UIButton(frame: CGRect(x: 0, y: -15, width: btnSz.width, height: btnSz.height))
Pbtn.setTitle("<<", for: UIControlState.normal)
Pbtn.backgroundColor = UIColor.clear
Pbtn.setTitleColor(UIColor.brown, for: UIControlState.normal)
Pbtn.titleLabel?.font = UIFont(name: enumFontNames.MarkerFelt_Wide.rawValue, size: 60.0)
Pbtn.isEnabled = false
Pbtn.isHidden = true
Pbtn.tag = enumBtnTag.tagPrev.rawValue
view.addSubview(Pbtn)
}
}
}
}
然后我捕获将要显示的页面。页面可能不会显示(用户拖动的不够),但稍后会处理。
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
if let identifier = pendingViewControllers[0].restorationIdentifier {
if let index = pages.index(of: identifier) {
pageNbr = index
}
}
}
现在我们修改didFinishAnimating中的按钮;
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if finished && completed {
if let button = self.view.viewWithTag(enumBtnTag.tagPrev.rawValue) as? UIButton {
if pageNbr > 0 {
button.isHidden = false
} else {
button.isHidden = true
}
}
if let button = self.view.viewWithTag(enumBtnTag.tagNext.rawValue) as? UIButton {
if pageNbr < pages.count - 1 {
button.isHidden = true
} else {
button.isHidden = false
}
}
}
}
红利代码:我在最后一页添加了一个保存功能,下一个按钮是。您需要设置按钮已启用(因此它会注册触摸)并设置一个目标(您想要执行的任何功能);我的是“下一个问题”。 当然,当不在最后一页时删除目标;
if pageNbr < pages.count - 1 {
//Not on last page. Use next button
button.setTitle(">>", for: UIControlState.normal)
button.removeTarget(self, action: #selector(nextSegue), for: UIControlEvents.touchUpInside)
button.isEnabled = false
} else {
//On last page. Use save button
button.setTitle("S", for: UIControlState.normal)
button.addTarget(self, action: #selector(nextSegue), for: UIControlEvents.touchUpInside)
button.isEnabled = true
}
希望这可以帮助别人。
你可以实现两个按钮,但只是为了清楚它是坏的UI设计,并不直观。 PageControl的设计目的就是像你拿在手中的牌一样,然后你翻过牌等等。 (不要点击另一张卡片)。 – 2011-03-21 09:37:35