我想知道如何添加按钮(左和右)来更改页面控件视图。 我正在研究这个教程[1]:http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/。除了此示例代码中的交换功能外,如何添加2个简单按钮(左和右)来翻页?
我是程序员初学者,所以任何一种答案都是高度评价的! :)
谢谢!
答案 0 :(得分:1)
您可以向视图添加两个按钮,当单击该按钮时,请根据单击的按钮调用方法来翻页。
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
}
答案 1 :(得分:0)
我会快速回答这个问题,但你应该能够翻译。
过程是找到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
}
}
}
}
奖金代码:我在最后一页添加了一个保存功能,其中包含下一个按钮。您需要设置按钮已启用(因此它会记录触摸)并设置目标(您想要执行的功能);我的是&#34; nextSegue&#34;。 当然,当不在最后一页时删除目标;
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
}
希望这有助于某人。