1014-26-homepage 07-Latest Weibo quantity ------- display the latest Weibo quantity animation --- tiled image --- array insertion array, 1014-26-07-
1. Integrate the built-in drop-down property control --------- HWHomeViewController
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Integrated refresh control
[Self setupRefresh];
}
/** Integrated drop-down refresh control */
-(Void) setupRefresh
{
// 1. Add a refresh control
UIRefreshControl * control = [[UIRefreshControl alloc] init];
// The UIControlEventValueChanged event is triggered only when you manually refresh it from the drop-down menu.
[Control addTarget: self action: @ selector (refreshStateChange :) forControlEvents: UIControlEventValueChanged];
[Self. tableView addSubview: control];
// 2. Enter the refresh status immediately (only displaying the refresh status does not trigger the UIControlEventValueChanged event)
[Control beginRefreshing];
// 3. load data immediately
[Self refreshStateChange: control];
}
Bytes ----------------------------------------------------------------------------------------------------------
/**
* UIRefreshControl enters the refresh status: load the latest data
*/
-(Void) refreshStateChange :( UIRefreshControl *) control
{
// 1. Request the Administrator
AFHTTPRequestOperationManager * mgr = [AFHTTPRequestOperationManager];
// 2. Splice Request Parameters
HWAccount * account = [HWAccountTool account];
NSMutableDictionary * params = [NSMutableDictionary dictionary];
Params [@ "access_token"] = account. access_token;
// Retrieve the top Weibo account (the latest Weibo account with the largest ID)
HWStatus * firstStatus = [self. statuses firstObject];
If (firstStatus ){
// If this parameter is specified, a microblog with a higher ID than since_id (that is, a microblog with a later ID than since_id) is returned. The default value is 0.
Params [@ "since_id"] = firstStatus. idstr;
}
// 3. Send the request
[Mgr GET :@ "https://api.weibo.com/2/statuses/friends_timeline.json" parameters: params success: ^ (AFHTTPRequestOperation * operation, NSDictionary * responseObject ){
// Convert the "Weibo Dictionary" array to the "Weibo model" Array
NSArray * newStatuses = [HWStatus objectArrayWithKeyValuesArray: responseObject [@ "statuses"];
// Add the latest Weibo data to the beginning of the total number Group
Nsange range = NSMakeRange (0, newStatuses. count );
NSIndexSet * set = [NSIndexSet indexSetWithIndexesInRange: range];
[Self. statuses insertObjects: newStatuses atIndexes: set];
// Refresh the table
[Self. tableView reloadData];
// End refresh and refresh
[Control endRefreshing];
// Display the number of latest Weibo posts
[Self showNewStatusCount: newStatuses. count];
} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){
HWLog (@ "request failed-% @", error );
// End refresh and refresh
[Control endRefreshing];
}];
}
/**
* Display the number of latest Weibo posts
*
* @ Param count the number of latest Weibo posts
*/
-(Void) showNewStatusCount :( int) count
{
// 1. Create a label
UILabel * label = [[UILabel alloc] init];
// ColorWithPatternImage: This method is used in Tiled scenarios.
Label. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "timeline_new_status_background"];
Label. width = [UIScreen mainScreen]. bounds. size. width;
Label. height = 35;
// 2. set other attributes
If (count = 0 ){
Label. text = @ "no new Weibo data. Try again later ";
} Else {
Label. text = [NSString stringWithFormat: @ "% d New Weibo data records in total", count];
}
Label. textColor = [UIColor whiteColor];
Label. textAlignment = NSTextAlignmentCenter;
Label. font = [UIFont systemFontOfSize: 16];
// 3. Add
Label. y = 64-label. height;
// Add the label to the view of the navigation controller and paste it under the navigation bar.
[Self. navigationController. view insertSubview: label belowSubview: self. navigationController. navigationBar];
// 4. Animation
// Use 1 s to move the label down
CGFloat duration = 1.0; // animation time
[UIView animateWithDuration: duration animations: ^ {
// Label. y + = label. height;
Label. transform = CGAffineTransformMakeTranslation (0, label. height );
} Completion: ^ (BOOL finished ){
// After a delay of 1 s, use 1 s to move the label up for a certain distance (back to the initial state)
CGFloat delay = 1.0; // delay 1 s
// UIViewAnimationOptionCurveLinear: constant speed
[UIView animateWithDuration: duration delay: delay options: UIViewAnimationOptionCurveLinear animations: ^ {
// Label. y-= label. height;
Label. transform = CGAffineTransformIdentity;
} Completion: ^ (BOOL finished ){
[Label removeFromSuperview];
}];
}];
// If you want to return to the State before the animation is executed after an animation is executed, we recommend that you use transform for the animation.
}