admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
//  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