//
|
// WeiKouAdView.m
|
// advertisement
|
//
|
// Created by weikou on 16/4/20.
|
// Copyright © 2016年 yuxingmin.com. All rights reserved.
|
//
|
|
#import "WeiKouAdView.h"
|
@implementation MyUIScrollView
|
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
{
|
if (!self.dragging)
|
{
|
[[self nextResponder] touchesBegan:touches withEvent:event];
|
}
|
[super touchesBegan:touches withEvent:event];
|
}
|
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
|
{
|
if (!self.dragging)
|
{
|
[[self nextResponder] touchesMoved:touches withEvent:event];
|
}
|
[super touchesMoved:touches withEvent:event];
|
}
|
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
|
{
|
if (!self.dragging)
|
{
|
[[self nextResponder] touchesEnded:touches withEvent:event];
|
}
|
[super touchesEnded:touches withEvent:event];
|
}
|
@end
|
|
|
@implementation WeiKouAdView
|
|
-(id)initWithFrame:(CGRect)frame withBlock:(viewBlock)block
|
{
|
self = [super initWithFrame:frame];
|
if (self) {
|
self.dataArray = [[NSMutableArray alloc]init];
|
[self setFrame:frame];
|
[self setBackgroundColor:[UIColor whiteColor]];
|
[self requestData];
|
|
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
|
imageView.userInteractionEnabled = YES;
|
//添加点击手势(包括单击和双击)
|
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
|
singleTapGesture.numberOfTapsRequired = 1;
|
singleTapGesture.numberOfTouchesRequired = 1;
|
[imageView addGestureRecognizer:singleTapGesture];
|
|
[self addSubview:imageView];
|
ablock = block;
|
|
}
|
if (_dataArray==nil) {
|
return nil;
|
}
|
return self;
|
}
|
|
-(void)setSuperView:(UIView *)superView
|
{
|
[superView addSubview:self];
|
|
}
|
-(void)requestData{
|
[[WeiKouNetWorkRequest sharedManager]requestDataWithMethod:@"requestad" withDevice:[NSString getIdentifierForAdvertising] withPlatform:@"ios" withDeviceName:[NSString getDevicename] withWidth:@"320" withHeight:@"480" withKw:@"" withKey:@"100012" returnBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
NSLog(@"%@",result);
|
_dataArray = [[NSMutableArray alloc]initWithArray:result[@"data"][@"adlist"]];
|
index = 0;
|
switchtime = [result[@"data"][@"switchtime"] integerValue];
|
if(_dataArray.count!=0){
|
[imageView sd_setImageWithURL:[NSURL URLWithString:[[_dataArray objectAtIndex:0] objectForKey:@"img"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
|
if (error==nil) {
|
[imageView setImage:image];
|
ablock(@"yuxingmin");
|
[[WeiKouNetWorkRequest sharedManager] upLoadDataWithMethod:@"reportad" withKey:@"100012" withContentid:[[_dataArray objectAtIndex:0] objectForKey:@"id"] withType:[NSNumber numberWithInteger:2] withDevice:[NSString getIdentifierForAdvertising] returnBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
}];
|
|
}else{
|
[imageView setImage:nil];
|
}
|
_timer = [NSTimer scheduledTimerWithTimeInterval:switchtime
|
target:self
|
selector:@selector(changeAdView)
|
userInfo:nil repeats:YES];
|
}];
|
}}
|
else
|
{
|
NSLog(@"%@",error);
|
}
|
}];
|
}
|
-(void)changeAdView
|
{
|
index = index+1;
|
if (index ==_dataArray.count) {
|
index=0;
|
}
|
UIImageView *tempImageView = [[UIImageView alloc] init];
|
[tempImageView sd_setImageWithURL:[NSURL URLWithString:[[_dataArray objectAtIndex:index] objectForKey:@"img"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
|
if (error == nil) {
|
[imageView setImage:image];
|
[[WeiKouNetWorkRequest sharedManager] upLoadDataWithMethod:@"reportad" withKey:@"100012" withContentid:[[_dataArray objectAtIndex:index] objectForKey:@"id"] withType:[NSNumber numberWithInteger:2] withDevice:[NSString getIdentifierForAdvertising] returnBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
|
}];
|
}
|
else
|
{
|
[imageView setImage:nil];
|
}
|
|
}];
|
}
|
|
-(void)closeButtonClick:(UIButton*)sender
|
{
|
[_timer invalidate];
|
_timer = nil;
|
[self removeFromSuperview];
|
|
|
}
|
|
-(void)handleSingleTap:(UITapGestureRecognizer*)singleTap{
|
if (_dataArray.count>index) {
|
NSString *url = [[_dataArray objectAtIndex:index] objectForKey:@"linkUrl"];
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
|
[[WeiKouNetWorkRequest sharedManager] upLoadDataWithMethod:@"reportad" withKey:@"100012" withContentid:[[_dataArray objectAtIndex:0] objectForKey:@"id"] withType:[NSNumber numberWithInteger:3] withDevice:[NSString getIdentifierForAdvertising] returnBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
|
}];
|
}
|
}
|
@end
|