Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
The introduction
Demo case functions:
- When entering the home page, the user is prompted to click tabBar again to refresh the interface data
- Refresh data while rotating the tabbar image
The demo from CSDN download the complete address: https://download.csdn.net/download/u011018979/15504711
Article syncs to # Applets: iOS Reverse, better reading experience and more please pay attention to # Applets: iOS Reverse, only for you to present valuable information, focusing on mobile technology research field.
Application scenario: It is applicable to tabBar on the home page of shopping app and shopping voucher app
Features: Rotates the tabbar icon during data updates
Video presentation from the blink view: blink.csdn.net/details/117…
I. Core implementation
When entering the home page, click tabBar again to refresh the interface data
1.1 Record the last button click in selectedViewController for data refresh
- Add a property that records the tag of the last button clicked
/** Records the tag of the last button clicked
@property (nonatomic.assign) NSInteger previousClickedTag;
Copy the code
1.2 implementation UITabBarControllerDelegate to refresh the data
- Set selectedViewController as the tabBarController’s delegate
self.tabBarController.delegate = self;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
Copy the code
- Record the last click of the button to refresh the data
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.view.backgroundColor = HWColor(245.245.245);
[self setNavigationContent];
// self.tabBarItem addObserver:<#(nonnull NSObject *)#> forKeyPath:<#(nonnull NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
self.tabBarController.delegate = self;
self.previousClickedTag = 100;// No tabbar is clicked by default
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 0 && [self.tabBarItem.title isEqualToString:homeTabbarSelectedTitle]) {
// Refresh the data
if ( self.previousClickedTag == tabBarController.selectedIndex ) {// A second click is made
[self.tableView.mj_header beginRefreshing]; }}self.previousClickedTag = tabBarController.selectedIndex;// Record the last button click
}
Copy the code
1.3 Implement dynamic replacement of UITabBarItem in the UITabBarDelegate proxy method
- Handles selected/unchecked UITabBarItem styles
Modify the title of the UITabBarItem with the didSelectItem proxy method to make the selected title different from the unselected title
- When switching to the home page, the title is refreshed, prompting the user to click TAB again to refresh the interface data
- If the home TAB is not selected, the title is the home page
NSString * const GYQhomeTabbarTitle = @ "home page";
NSString * const GYQhomeTabbarSelectedTitle = @ "refresh";
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
if ([item.title isEqualToString:GYQhomeTabbarTitle]) {
item.title = GYQhomeTabbarSelectedTitle;
}else{
// Change the title of the home Tab to "Home" when switching to other sub-tabs.
for (UITabBarItem *childView in tabBar.items) {
if([childView.title isEqualToString:GYQhomeTabbarSelectedTitle] && childView ! = item) { childView.title = GYQhomeTabbarTitle; }}}/ / -- -- -- -- -- -- -- --
// Copyright notice: This article is an original article BY CSDN blogger "# public account: iOS Reverse", in accordance with CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
/ / the original link: https://blog.csdn.net/z929118967/article/details/77885824
}
Copy the code
II. Rotate the tabbar image
/** Whether to refresh tabbar images */
@property (nonatomic.assign) BOOL isreloadData;
/ * * stored UITabBarSwappableImageView for rotating tabbar picture * /
@property (nonatomic.strong) UIView *imageView;
Copy the code
2.1 Customize UITabBar to listen for click events
- Listen for the click event of the UITabBar and pass the view of the icon to the periphery to animate the rotation
// Iterate over the tabBar child controls and bind the animation event to the button of type "UITabBarButton"
//(Note: the time to iterate over adding an animation event is in the layoutSubviews layout child control method)
- (void)layoutSubviews{
[super layoutSubviews];
for (UIControl *tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside]; }}}// Animation effect (traverses the child control of UITabBarButton,
/ / if you need to add animation images, search for "UITabBarSwappableImageView" type of picture child controls;
//// If you need to animate the text below the button, look for a text child control of type "UITabBarButtonLabel").
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
for (UIView *imageView in tabBarButton.subviews) {
if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
if (self.block) {
self.block(imageView); }}}}Copy the code
2.2 Rotate the icon of tabbar during data update
/** Whether to replace tabbar image */
@property (nonatomic.assign) BOOL isreloadData;
Copy the code
- Rotate the tabbar image
self.previousClickedTag = 100;// No tabbar is clicked by default
HWTabBar *tabBar = (HWTabBar*)self.tabBarController.tabBar;
[tabBar setBlock:^(UIView * imageView){
if (self.isreloadData) {
self.imageView = imageView;
CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; // Rotate it around the z axis
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];// Rotate the Angle
rotationAnimation.duration = 2; // Rotation period
rotationAnimation.cumulative = YES;// Rotation accumulation Angle
rotationAnimation.repeatCount = 100000;// Number of rotations
[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
}];
}
-(void)stopRotate {
[self.imageView.layer removeAllAnimations];
}
- (void)setIsreloadData:(BOOL)isreloadData{
_isreloadData = isreloadData;
if(! isreloadData) { [selfstopRotate]; }}Copy the code
- Refreshing data
#pragmaMark-tab click refresh related
/** If other child TabbarControllers are also ViewController objects, self.tabbarItem. title will be retrieved incorrectly */
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 0 && [self.tabBarItem.title isEqualToString:GYQhomeTabbarSelectedTitle]) {//// //(lldb) po [[self tabBarItem] title]
// Refresh the data
if ( self.previousClickedTag == tabBarController.selectedIndex ) {// A second click is made
self.isreloadData = YES;
// [tablview.mj_header beginRefreshing];
[self.webView reload];
// Remove icon rotation animation after data update}}self.previousClickedTag = tabBarController.selectedIndex;// Record the last button click
}
Copy the code
- Remove icon rotation animation after data update
- (void)webViewControllerDidFinishLoad:(AXWebViewController *)webViewController; {[self stopRotate];
}
- (void)webViewController:(AXWebViewController *)webViewController didFailLoadWithError:(NSError *)error
{
[self stopRotate];
}
Copy the code
III. Precautions
3.1 Precautions for Data Update
Check if the number of elements storing the data model array is empty when switching between views
- To ensure that data is requested the first time the TAB is switched, we can determine whether to request data based on the number of elements in the model array.
#warningCheck if the number of elements storing the data model array is empty when switching between views
if (self.titleHotIndexmodel.Count == 0) {make a request}Copy the code
3.2 implementation of data refresh of UITabBarControllerDelegate matters needing attention
If other child TabbarControllers are also ViewController objects, self.tabbarItem. title is incorrectly fetched. Therefore, it is recommended that different tabbarControllers use different classes
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//(lldb) po [[self tabBarItem] title]
Copy the code
see also
For more, check out # Applets: iOS Reverse, which presents valuable information only for you, focusing on the mobile technology research field.